shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

MqttConnectionStatus is not updating to DISCONNECTED #227

Closed Talles71 closed 4 years ago

Talles71 commented 4 years ago

Hello everyone,

In my flutter app the user can acess multiple screen that reflect real time data coming from mqtt broker. Each screen i'm doing on dispose MQTT disconnect and on init state force disconnect again and MQTT Connect.

On lost connection with the broker the MqttConnectionStatus is not updating to DISCONNECTED or the callback _onDisconnect not triggering and the screen show old data, just when i do any action like change screen to update the connection status.

In my broker server console shows "SOCKET ERROR DISCONNECTING..." for flutter client.

I'm looking the code of mqtt client if i can do anything about messageStream onError, onDone or onData

Somebody can help me ?

shamblett commented 4 years ago

I'm not sure why you are doing this. The client was not designed to be created and destroyed repeatedly, you are supposed to create the client, connect it and leave it, its the data you need to distribute across your screens. If you look through the closed issues you'll see other flutter users have encountered this and fixed it. I'm not a flutter user so I can't help you here.

This line seems wrong 'dispose MQTT disconnect and on init state force disconnect again and MQTT Connect.' why are disconnecting again? Like I say above you shouldn't do this anyway.

Talles71 commented 4 years ago

Dear shambler, thank you for the answer.

You are right about disconnect / connect on init and on dispose. Now i connect on mqtt after the user logged in, and the app keep the connection between screens.

I'm using stream every second observing for MqttConnectionStatus and now i think this solve the problem.