Skip to content Skip to sidebar Skip to footer

Reliable WebSocket Connection State Detection

I've been looking around to implement a reliable WebSocket connection recovery mechanism. After some investigation, I've found that one way is sending hearbeats to the server (ping

Solution 1:

The websocket protocol define special control frames for ping and pong, but they are not accessible through the JavaScript API. But if the server send those frames, the browser will answer.

However, if the connection is cut suddenly and become an half-open connection, although the server will detect it, the browser won't. So I guess that sending your own pings at application level is not a bad idea.

Answering your question yes, it is good idea because if the connection gets half opened, your client is not getting updates because he thinks it is connected. In websocket, the client has to initiate the connection, so even if the browser realizes the disconnection, there is nothing it can do to reconnect.


Post a Comment for "Reliable WebSocket Connection State Detection"