shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

WebSocket is already in CLOSING or CLOSED state. #428

Closed divyeshDev closed 1 year ago

divyeshDev commented 1 year ago

I used this mqtt_client package. Every time after 20-25 minutes WebSocket WebSocket is already in CLOSING or CLOSED state. gives an error.

Any suggestions on this issue? How to fix this problem?

OS : MacOs Broswer : Chrome, safari

Screenshot 2022-12-15 at 11 38 14 AM

My code snippet below :

  void _setUpMqttClient() {
    _url = _prepareWebSocketUrl();
    _client = mqttsetup.setup(_url, _clientId);
    // Set the protocol to V3.1.1 for AWS IoT Core, if you fail to do this you will not receive a connect ack with the response code
    _client.setProtocolV311();
    _client.logging(on: _logging);
    _client.autoReconnect = true;
    _client.port = 443;
    _client.keepAlivePeriod = 60;
    _client.disconnectOnNoResponsePeriod = 1;
    _client.onDisconnected = awsInterface.onDisConnected;
    _client.onConnected = awsInterface.onConnected;
    _client.onSubscribed = awsInterface.onSubScribed;
    _client.onSubscribeFail = awsInterface.onSubscribeFail;
    _client.onUnsubscribed = awsInterface.onUnsubscribed;
    _client.onAutoReconnect = awsInterface.onAutoReconnect;
    _client.onAutoReconnected = awsInterface.onAutoReconnected;
    _client.resubscribeOnAutoReconnect = true;
    _client.connectionMessage =
        MqttConnectMessage().withClientIdentifier(_clientId).startClean();
  }
shamblett commented 1 year ago

OK, I'll need a client log to see whats happening here although this particular problem hasn't been reported before.

divyeshDev commented 1 year ago

Hello, @shamblett

please look client logs:

Screenshot 2022-12-28 at 1 30 37 PM

shamblett commented 1 year ago

The failure is in the connectAuto method so you must have lost your connection and triggered auto reconnect. It should stay in auto reconnect until it connects, does it eventually connect if you leave it long enough? I'll check the code to make sure we are recycling the socket.

divyeshDev commented 1 year ago

@shamblett For more than 30 minutes we do nothing on the web. Then we receive WebSocket error.

shamblett commented 1 year ago

Yes becsuse the network has disconnected you and cos u have autoreconnect set the client tries to reconnect, this is where the error appears.

If you do nothing and let the client just run dors it eventually re connect(assuming your network comes back of course) ?

Its also possible your broker has gone down but this is rarer.

divyeshDev commented 1 year ago

No, I didn't turn off the network. the network is continuously on my pc.

shamblett commented 1 year ago

Ok, in that case when the autoreconnect sequence starts can you still ping your broker?

divyeshDev commented 1 year ago

no, not. @shamblett please see attached photo

Screenshot 2022-12-28 at 4 33 51 PM

shamblett commented 1 year ago

No sorry I didnt mean from the client, the client is not connected so it will not send keep alive pings I meant the standar networking ping command at the cli or any other way u may want to verify ur broker is still reachable.

MurtuzaSrashtaSoft commented 1 year ago

Client is already reachable. how we tell. when i do browser refresh button in flutter hole application reinitialised and at that time new mqtt instance is created and that is working fine.

So broker is all time reachable and it's aws iot broker.

if i checked on mqttbox i able to connect with wss protocol.

shamblett commented 1 year ago

OK thanks for checking, what we need to know now is why auto reconnect is being triggered in the first place, i.e. why we have(or think we have) lost connection to the broker in the first place.

I need the client log from just above where autoreconnect is triggered to see why. the log you have supplied shows the client when its already in autoreconnect, this is too late.

divyeshDev commented 1 year ago

hello, @shamblett please look

2023-01-04 14:56:02.446 -- MqttBrowserWsConnection::connect - websocket is open 2023-01-04 15:14:53.868 -- MqttBrowserConnection::_startListening - websocket is closed

Screenshot 2023-01-04 at 3 55 42 PM

[Question]: is there any callback available that tells WebSocket goes into the reconnected(MqttBrowserWsConnection::connectAuto - WebSocket has errored) mode?

shamblett commented 1 year ago

The first two lines, show that at 14:56 you connected and at 15:14 the socket closed for some reason as you have spotted, so you have lost connection at the network level which is triggering auto reconnect, this should work however. There is an on Autoreconenct callback that is triggered when autoreconnect kicks in, there is also one when autoreconnect has completed, please see the API docs.

MurtuzaSrashtaSoft commented 1 year ago

ok. and how we can identifying why it's goes to reconnect. and why auto reconnect not able to connect successfully?

shamblett commented 1 year ago

Unfortunately the client cant tell you why it was disconnected as youvr seen from the logs the client just getsca socket event from the Dart runtime saying the socket has closed its not told why.

Autoreconnect should work however Ill do some testing to see if we have bug here for browser based connections.

MurtuzaSrashtaSoft commented 1 year ago

@shamblett

and this is heppend only on web environment in release mode in debug mode it's working fine.

now i don't have a any why how to resolve this issue.if you have any suggestion kindly share with us.

shamblett commented 1 year ago

When you say 'working fine' do you mean in debug mode when the network disconnects and autoreconnect starts then the client does eventually re-connect or are you saying that the client doesn't disconnect in the first place?

The client package has no concept of debug or release mode, its just Dart code so whatever is happening I doubt if the client can help you with it.

MurtuzaSrashtaSoft commented 1 year ago

in debug mode client doesn't disconnect in the first place.

yes i know. but i share whatever i observed.

divyeshDev commented 1 year ago

@shamblett Any update?

is there any way to resolve this still we have received this WebSocket error?

Is it possible that when MQTT disconnects, it can connect automatically? Is there any solution?

ashley-arta commented 1 year ago

Hi @shamblett I'm facing the same issue on web too. It's not possible to connect with the same client identifier again

image the error stack points to this line https://github.com/shamblett/mqtt_client/blob/master/lib/src/connectionhandling/browser/mqtt_client_mqtt_browser_ws_connection.dart#L224

ashley-arta commented 1 year ago

sorry my errors were actually caused by the Backend 🙇 kubernetes were down

uberchilly commented 10 months ago

This happens on Chrome-based browsers (edge, chrome...) after the tab is inactive for some time, It is some sort of optimization from Chrome, it doesn't happen on Mozilla and you cannot reproduce it (at least I think) in debug mode for some reason https://solace.community/discussion/694/client-websocket-disconnects-when-browser-is-minimized-or-not-in-focus-chrome