Search code examples
node.jssocket.ioheartbeat

node.js sockets.io disconnecting every ~25 seconds (heartbeat-related)


I'm trying to set up a node.js server, using socket.io. The problem I'm seeing is that my server is disconnecting+reconnecting the client every 25 seconds.

Here's my server code:

var io = require('socket.io').listen(5876);
io.sockets.on('connection', function (socket)
{
    console.log("New connection established");

    socket.on('disconnect', function()
    {
        console.log("A client has disconnected.");
    });
}

My client connects using the socket.io.js distribution. As I understand it (apparently incorrectly), I need my client to send a "heartbeat" (a message of "::2") every 15 seconds to prevent the server from thinking the connection is dead and disconnecting it. Basically:

<script src="socket.io.js"></script>
<script>
    var socket = io.connect('http://localhost:5876');

    socket.on('connect', function(data)
    {
        console.log("Connected to server.");
        setInterval(function() { socket.emit("::2"); console.log("sent heartbeat"); }, 15000); // schedule a heartbeat for every 15 seconds
    });
</script>

But the client is still getting disconnected+reconnected every 25 seconds (excluding the very first 25th-second-tick).

The node.js server console log looks like this (may have cut out some earlier identical connect/disconnect phases, as it was echoing every 25 seconds):

New connection established
   debug - emitting heartbeat for client 652791160849839915
   debug - websocket writing 2::
   debug - set heartbeat timeout for client 652791160849839915
   debug - got heartbeat packet
   debug - cleared heartbeat timeout for client 652791160849839915
   debug - set heartbeat interval for client 652791160849839915
   info  - transport end
   debug - set close timeout for client 652791160849839915
   debug - cleared close timeout for client 652791160849839915
   debug - cleared heartbeat interval for client 652791160849839915
A client has disconnected.
   debug - discarding transport
   debug - client authorized
   info  - handshake authorized 5961067041159055274
   debug - setting request GET /socket.io/1/websocket/5961067041159055274
   debug - set heartbeat interval for client 5961067041159055274
   debug - client authorized for
   debug - websocket writing 1::
New connection established

How can I stop my server from disconnecting+reconnecting the client every 25 seconds?


Solution

  • Are you using the latest version of socket.io, 0.9.1/0.9.1-1? If so, this is a known issue confirmed by Guillermo. The current recommendation is to roll back to 0.9.0 or 0.8.7

    https://github.com/LearnBoost/socket.io/issues/777