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?

Josh1billion
  • 14,554
  • 8
  • 33
  • 45
  • 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. – cillierscharl Mar 05 '12 at 10:06
  • 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 3744936361854682925 – sudoExclaimationExclaimation Sep 11 '16 at 19:31
  • @PranoyC There's a workaround here that might work: https://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. – Josh1billion Sep 11 '16 at 21:52
  • @Josh1billion 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 workaround – sudoExclaimationExclaimation Sep 11 '16 at 22:20
  • @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. – Josh1billion Sep 14 '16 at 16:40
  • @Josh1billion okay, thank you! – sudoExclaimationExclaimation Sep 14 '16 at 18:02

2 Answers2

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

CoderMD666
  • 907
  • 1
  • 7
  • 16
  • 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" – Josh1billion Mar 05 '12 at 20:16
  • 1
    You can specify a specific version to install: npm install socket.io@0.9.0 – CoderMD666 Mar 06 '12 at 03:59
  • having same issue but rolling back had no effect – user2005121 Dec 05 '13 at 15:10
  • 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. – klode Jan 29 '14 at 18:50
  • 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 3744936361854682925 – sudoExclaimationExclaimation Sep 11 '16 at 19:31
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"});
Jude Calimbas
  • 2,385
  • 1
  • 23
  • 24