pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.22k stars 197 forks source link

Heartbeat check supprortment #98

Open RolkerMan opened 5 years ago

RolkerMan commented 5 years ago

It seems that the library determines to reconnect by listening to the close() event . Based on a test in chrome, in a case when you cut the internet connection, the close event would be triggered after about 50 seconds. That's to long for the app to start reconneting websocket and notifying users. So, I think it's crucial to provide a heatcheck api which helps checks whether the websocket was disconnected in time. Are there any misunderstandings I have on the reconnecting-websocket library or is heartbeat check already supported?

teneon commented 5 years ago

In node JS environment, using ReconnectigWebsocket does not detect 'close' event if i pull out network cable. Also 'ping' method is unsupported, therefore one cannot create heartbeat system easily. Hopefully this feature will be added in the future, i find it important and probably others as well. If the purpose of the library is to handle reconnections, it should handle as much cases as possible, also network disconnects i believe. Otherwise it would be a solid library, but right now i will have to switch back to standard WebSocket library and implement heartbeat (ping/pong) myself. It won't be as robust probably, so it is a pitty :/

pladaria commented 5 years ago

What API do you propose for this?

maybe a new option like:

{
    heartbeat: {message: 'ping', delay: 5000, timeout: 1000},
}

This would send a "ping" message after 5000 ms of inactivity and, if no response is received after 1000 ms, tries to reconnect.

Any server response could be considered as a valid response to clear interval (this proves the connection is up)

yuu2lee4 commented 5 years ago

need this feature

RolkerMan commented 4 years ago

What API do you propose for this?

maybe a new option like:

{
    heartbeat: {message: 'ping', delay: 5000, timeout: 1000},
}

This would send a "ping" message after 5000 ms of inactivity and, if no response is received after 1000 ms, tries to reconnect.

Any server response could be considered as a valid response to clear interval (this proves the connection is up)

would be a useful one!

rpocase commented 4 years ago

Anything new on this? Would be great to remove some extra boilerplate.

hernyo commented 4 years ago

{ heartbeat: {message: 'ping', delay: 5000, timeout: 1000}, }



This would send a "ping" message after 5000 ms of inactivity and, if no response is received after 1000 ms, tries to reconnect.

Are you talking about sending a regular message containing the "ping" string, or sending a ping ICMP message? There is a slight controversy on which one is the correct solution to send keepalives / heartbeats: pings should be the "normal" way but they only detect if the connection is up, they don't detect if the server is actually responsive. Also, the browser's built-in WebSocket implementation does not expose the ping method.

Any server response could be considered as a valid response to clear interval (this proves the connection is up)

Many servers rely on ICMP ping / pong to periodically check and and make sure the client is still connected. These pings should be handled just in the same way as regular messages since they indicate the connection is still fine. However, as mentioned above, not all WebSocket implementations support listening to pings.

This library exposes the ping / pong methods: https://github.com/websockets/ws

Other than this, I subscribe that this would be a great feature.

happy15 commented 3 years ago

any progress on this issue?

npomfret commented 3 years ago

I'm trying to use this to connect to Binance, but i think the lack of a ping response is resulting in my connection stopping (https://binance-docs.github.io/apidocs/spot/en/#websocket-market-streams)