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

No error fired when the network is off #247

Closed
theRenard opened this issue Jun 15, 2020 · 10 comments
Closed

No error fired when the network is off #247

theRenard opened this issue Jun 15, 2020 · 10 comments
Milestone

Comments

@theRenard
Copy link

theRenard commented Jun 15, 2020

Hi, I think I'm missing some fundamentals about how stomp-js works with websockets and when it fires errors. I've set an heartbeat (incoming / outcoming ) on both server and client of 1000 milliseconds and the PING / PONG works fine. Now, when I set my network off (so the remote server is totally unreachable) I expect at least one of these errors : onWebSocketClose, onWebSocketError, onDisconnect, onStompError but none fires. I can see the message about the heartbeat ("did not receive server activity for the last XXXXms") but nothing else.

Strange enough, when I set the network on again it fires onWebSocketClose once (just before the connection is re-established).

did not receive server activity for the last 12333ms
Connection closed to wss://xxx.yyy.xxxx-xxxx.xyz/api/ws
onWebSocketClose (triggered by a console.log @onWebSocketClose)
STOMP: scheduling reconnection in 500ms
Opening Web Socket...
Web Socket Opened...
>>> CONNECT
accept-version:1.0,1.1,1.2
heart-beat:1000,1000

Received data
<<< CONNECTED
heart-beat:1000,1000
version:1.2
content-length:0

Can you point me to some documentation/examples ?

// EDIT

It looks like _websocket.close() never happen, (shouldn't it be fired just after the first debug line ?)

2 >>> PING
[here I turn off my wi-fi, remote STOMP server is unreachable]
did not receive server activity for the last 3238ms
did not receive server activity for the last 4474ms
did not receive server activity for the last 5706ms
did not receive server activity for the last 6938ms
did not receive server activity for the last 8174ms
did not receive server activity for the last 9408ms
did not receive server activity for the last 10641ms
did not receive server activity for the last 11875ms
did not receive server activity for the last 13109ms
did not receive server activity for the last 14345ms
did not receive server activity for the last 15576ms
did not receive server activity for the last 16810ms
did not receive server activity for the last 18047ms
[here I turn on my wi-fi, remote STOMP servers is reachable again]
Connection closed to wss://xxx.yyy.xxxx-xxxx.xyz/api/ws
onWebSocketClose
STOMP: scheduling reconnection in 5000ms
@kum-deepak
Copy link
Member

The .close is issued just after the debug message (https://github.com/stomp-js/stompjs/blob/master/src/stomp-handler.ts#L261). However the socket does not immediately close.

I have been struggling with this for a while now. The actual time taken to close the socket after issuing .close varies widely depending on circumstances and specific browser - sometimes 1-2 minutes as well. Unfortunately, I have not been able to figure a work around.

@theRenard
Copy link
Author

This is really interesting indeed...

So, as the STOMP specs states that:

if, inside a time window of at least milliseconds, the receiver did not receive any new data, it MAY consider the connection as dead

why don't fire a disconnection method and/or expose events like onConnectionStale or onDisconnect when the heartbeatIncoming delay is over the permitted delta ? Actually there isn't any way to easily tell if the server communication is broken/stalled when the network is off (in my implementation even few milliseconds of delay are critical).

@kum-deepak
Copy link
Member

I see one possible workaround. When the web socket is closed after no heartbeat, all even handlers are set to no-op on the socket and assume WebSocket to be closed. This will allow to attempt a new connection.

Please let me know if you would like to try this approach.

@theRenard
Copy link
Author

What you suggest is a possible workaround on the existing library ? If it's the case, could you please show me how the client instance could be notified about the state change ?

In any case I'll gladly test your approach

@kum-deepak
Copy link
Member

I have created PR #250, please see the diff. Set client.discardWebsocketOnCommFailure = true.

Install from https://github.com/stomp-js/stompjs/raw/discardSockets/stomp-stompjs-5.4.5.tgz.

Please let me know how it goes. I have also added a debug statement of actual socket closure.

@robot-tin
Copy link

Brilliant! It works as expected 🙆‍♂️.

Thank you very much 👍

@kum-deepak
Copy link
Member

Can you please attach your log file. I want to see how it behaves in the real life.

@robot-tin
Copy link

As you can see once the network (wi-fi) is back again the websocket connection is finally closed (before attempting a new connection) So if fires the onWebSocketClose callback twice. This is a non-issue for me because it just put the app on the same state again.

Opening Web Socket...
Web Socket Opened...
>>> CONNECT
accept-version:1.0,1.1,1.2
heart-beat:100,100


Received data
<<< CONNECTED
heart-beat:1000,1000
version:1.2
content-length:0


connected to server undefined
send PING every 1000ms
check PONG every 1000ms
>>> PING
Received data
<<< PONG
>>> PING
Received data
<<< PONG
>>> PING
Received data
<<< PONG
>>> PING
Received data
<<< PONG
3>>> PING
// HERE I FORCE THE WI-FI DISCONNECTION
did not receive server activity for the last 2840ms
Discarding websocket, the underlying socket may linger for a while
Connection closed to wss://XXXXXXXX/api/ws
console.log => onWebSocketClose
STOMP: scheduling reconnection in 5000ms
Opening Web Socket...
// HERE I RESTART THE WI-FI CONNECTION
Connection closed to wss://XXXXXXXX/api/ws
console.log => onWebSocketClose
STOMP: scheduling reconnection in 5000ms
Discarded socket closed after 7082ms, with code/reason: 1002/
Opening Web Socket...
Web Socket Opened...
>>> CONNECT
accept-version:1.0,1.1,1.2
heart-beat:100,100


Received data

@kum-deepak
Copy link
Member

Thanks, I want to also see how it works when the connectivity is down for a while. To simulate please set the reconnectDelay to 500ms and keep the WiFi off for few minutes.

@kum-deepak kum-deepak added this to the 6.x milestone Sep 21, 2020
@kum-deepak
Copy link
Member

Released as part of 6.0.0.

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

3 participants