Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnection from MQTT and Failure to Reconnect #700

Open
amin78tech opened this issue Dec 14, 2024 · 1 comment
Open

Disconnection from MQTT and Failure to Reconnect #700

amin78tech opened this issue Dec 14, 2024 · 1 comment

Comments

@amin78tech
Copy link

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()
	}
@MattBrittan
Copy link
Contributor

I’ve implemented logic to reconnect

Why? The library already includes this functionality (automatic reconnect).

With regards to the disconnections (which appears to be your real issue) you have not provided enough info to enable anyone to help (see the readme re common issues, where to ask for help, and what info to provide - in this case a minimal, reproducable, example is really needed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants