7skies
7skies

Reputation: 177

Reconnect websocket/mqtt after client internet disconnection

I am setting up an MQTT/Websockets server, my client is an flutter app, which connects to the broker on main screen, and in other screens it sends and receive messages from the broker. My understanding of keepAlive is how often the client and server should share ping/pong, so they make sure the connection is still alive. being said, if my flutter app, connects to the broker in main screen, of 3600/1 hour keepAlive, and suppose to share and receive messages on other screens, if i disconnect the client from the internet for 2 minutes, and reconnect after that, it will not send/receive messages, maybe my understanding of keepAlive is not correct. Well, How would i structure my app/server to reconnect automatically to the internet as soon as internet connection is back and up again.

I have also tried On.Disconnect method, which i noticed it will never get called, and the app even though still thinks its connected to the broker. I mentioned websockets, on the tags as i could do mqtt over websockets.

Upvotes: 0

Views: 1930

Answers (1)

dls49
dls49

Reputation: 67

I see that no-one else has responded, so I'll try (however I'm new to this also).

Also, have you looked at the Flutter connectivity package?

From my reading of the Mqtt specification, it seems the Mqtt client ** should** disconnect the TCP/IP connection if it doesn't receive a PINGRESP to its PINGREQ in the keep alive period (ie it's not required to disconnect).

My Flutter + Mqtt app checks the connection state, and reconnects if needed, every time it sends a message. I haven't needed to check for internet dropouts, but I have noticed the connection is lost on some application state changes. The main app widget. is notified of these using didChangeAppLifecycleState() and sends a dummy message if needed.

So this doesn't answer exactly what you asked, but I hope it's useful anyway.

Upvotes: 0

Related Questions