We have seen more and more cases where client side want to keep the websocket connection with node to live indefinitely to receive occasional message. However, currently when the connection is idle for a long time, it might have been lost silently, and the client side will not know it without trying to send a message. D-chat, nkn-shell and a few other apps all suffer with this problem and has to add some (common) application layer logic to handle keepalive on the application side.
There is a ping/pong protocol in websocket, but the API is not well supported or exposed. For example, in browser, there is no API to send ping message, or getting notified when receiving ping message. So the websocket ping/pong protocol is not as cross platform as we want.
What’s being proposed here is using a special message sent to its own NKN address through the connecting node as keepalive message. It could be a new message type together with the existing type BINARY, TEXT and ACK. If a client is able to send ping and receive pong as reply, it means the client-node connection is good, and the node is able to respond to message. The protocol can be described as follows:
- Send a ping message to its own NKN address every INTERVAL (e.g. 20s)
- Reply a pong message to ping message sent by itself (we can probably allow responding to ping message sent by other people)
- If a client fails to receive any pong response for TIMEOUT (e.g. 60s), it will enter the reconnect logic
If this protocol is implemented, application will be able to enjoy a much more reliable and long-lived websocket connection, especially for client that mostly often receive but not send message.