Skip to content

Disconnection from MQTT and Failure to Reconnect #700

@amin78tech

Description

@amin78tech

Hi everyone, I’m facing an issue where I get disconnected after connecting. In my code, I’ve implemented logic to reconnect if it gets disconnected, but it keeps disconnecting again.
I need to subscribe to multiple topics and process their payloads. Could someone please help me out?

go func() {
		for {
			if !client.IsConnectionOpen() {
				fmt.Println("Client is not connected!")
				token := client.Connect()
				token.Wait()
			} else {
				fmt.Println("mqtt is connect :)")
			}
			time.Sleep(1 * time.Second)
		}
	}()

or in publish

if mqttClient.IsConnectionOpen() {
		tokenContent := mqttClient.Publish(contentTopic, byte(0), false, string(deCodeBase64))
		tokenContent.Wait()
	} else {
		fmt.Println("mqtt connect fail 4")
		ConnectWait := mqttClient.Connect()
		ConnectWait.Wait()
		fmt.Println("connect after fail 4 successfully")
		tokenContent := mqttClient.Publish(contentTopic, byte(0), false, string(deCodeBase64))
		tokenContent.Wait()
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions