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

2

8

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?

Resuscitate answered 5/3, 2012 at 9:55 Comment(6)
as far as i know you dont need to manually send heartbeat packets as this is handled by socket.io. Try checking the connection and disconnection events after taking out your manual heartbeat calls.Badillo
did you ever resolve this? i am having the exact issue. I downgraded to 0.8.7 and still same issue. debug - fired heartbeat timeout for client 3744936361854682925 info - transport end debug - set close timeout for client 3744936361854682925 debug - cleared close timeout for client 3744936361854682925Joyjoya
@PranoyC There's a workaround here that might work: github.com/socketio/socket.io/issues/777 Aside from that, I've lately just been using the newest versions of socket.io and haven't run into any problems.Resuscitate
@Resuscitate unfortunately my project can't use anything newer than 0.9.x as 1.0 changes a lot of things which completely break our project. I will look at the workaroundJoyjoya
@PranoyC Dang, hopefully the workaround works then. If not, the next thing I'd probably try is to downgrade even further than 0.8.7 and hope that some older version doesn't have the bug. Aside from that, I'm afraid I have no idea.Resuscitate
@Resuscitate okay, thank you!Joyjoya
M
9

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

Maladapted answered 5/3, 2012 at 12:39 Comment(5)
That must be it; I just installed it last night, so it's probably the latest version. How can I roll back to 0.9.0? I installed using the recommended npm method like this: "npm install socket.io"Resuscitate
You can specify a specific version to install: npm install [email protected]Maladapted
having same issue but rolling back had no effectPisano
Same thing here, with socket 0.9.16, and 0.9.0, I get 'transport end (heartbeat timeout)' a minute or so after starting the app. With socketio 0.8.7 I get a sequence of debug- set/cleared heartbeat time out for client.Saddlebacked
i am having the exact issue. I downgraded to 0.8.7 and still same issue. debug - fired heartbeat timeout for client 3744936361854682925 info - transport end debug - set close timeout for client 3744936361854682925 debug - cleared close timeout for client 3744936361854682925Joyjoya
P
2

you dont need to send a heartbeat to keep the socket connected, also the syntax of socket.emit is

          socket.emit('event_name',{"data"});
Pyrethrin answered 5/3, 2012 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.