Search code examples
wakanda

Wakanda websocket onclose not firing on network disconnect


I have a websocket implementation on Wakanda Server. I am tracking connected browsers server side. When I reload the page, I get the onclose event of the websocket as expected.

I would have expected when I break a network connection that I would also have this event fire. Am I misunderstanding? Is there a way to have a function fire when a network connection is lost to a websocket client?

For reference, I am using Wakanda 10.


Solution

  • To help you understand the problem I will give you some details about websocket connections :

    • It uses TCP
    • If a remote client(browser for instance) closes a TCP connection gracefully the server is notified (FIN)
    • If a remote client crashes, the OS running the client application notifies the server (RST)
    • If a TCP connection is open between a server and a client the connection will be assumed open even if there is absolutely no exchange of data for a while unless keepalive is activated.
    • The WebSocket spec adds a close message so the server/client can handle a graceful close
    • The WebSocket spec adds a ping/pong message exchange but does not impose a timeout. From RFC : Upon receipt of a Ping frame, an endpoint MUST send a Pong frame in response, unless it already received a Close frame. It SHOULD respond with Pong frame as soon as is practical.
    • In Wakanda's implementation of the RFC we respond to received ping messages but don't send our own ping messages

    This can be a new feature where we can either :

    • expose a ping method through the message port with a pong timeout
    • add a websocket keepalive option with configurable timeout at the moment of adding the websocket handler.
    • add a TCP keepalive option with configurable timeout at the moment of adding the websocket handler.

    For the time being, you can send a message of your choice to the client side every N seconds and wait for a response with a timeout of T. If you don't receive a response you can assume the connexion is lost and close it.