NodeJS Einaros WS Connection Timeout

I am using Einaros WS module with Node JS on one computer (server) and it works okay when I connect using another (client) computer.

If I plug the network cable, the ws.on('close', function()... is not firing and am looking for advice whether:

  • I have to implement my own ping/pong, or

  • There is a built-in feature to handle this automatically.

Einaros WS does have the ability to send Ping and Pong frames that are understood by most browsers and socket frameworks out there. I've tested with Socket Rocket on iOS and it had no issues understanding Ping frames for Einaros. You do have to come up with the application specific logic of how often to ping, as well as how many missed pongs you will tolerate. Einaros WS has ping() and pong() functions to send pings or pongs. You listen to the "pong" event to know when you received a response from the clients. Here's how I do this in my code:

wss.on("connection", function(ws) {
console.log("websocket connection open");

ws.pingssent = 0;

var id = setInterval(function() {

if (ws.pingssent >= 2)   // how many missed pings you will tolerate before assuming connection broken.
    {
        ws.close();
    }
else
    {
        ws.ping();
        ws.pingssent++;
    }
}, 75 * 1000);   //  75 seconds between pings

 ws.on("pong", function() {    // we received a pong from the client.
    ws.pingssent = 0;    // reset ping counter.
});

In the case of Socket Rocket, no code was required on the client side. So compatible browsers and clients will respond with Pongs automatically.