Node.js sockets.io disconnecting every ~ 25 seconds (associated with a heartbeat)

I am trying to configure node.js server using socket.io. The problem that I see is that my server disconnects + reconnects the client every 25 seconds.

Here is 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 wrong), I need my client to send a "heartbeat" (message ":: 2") every 15 seconds so that the server does not think that the connection is dead and disconnected. Mostly:

 <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 still disconnects + reconnects every 25 seconds (excluding the very first 25th-second-tick).

The node.js server console log looks like this (it may have cut out several previously identical connection / disconnect phases since it repeated 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?

+8
heartbeat
source share
2 answers

Are you using the latest socket.io version, 0.9.1 / 0.9.1-1? If so, this is a known issue confirmed by Guillermo. The current recommendation is rollback to 0.9 or 0.8.

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

+9
source share

you do not need to send a pulse to support the socket, as well as socket.emit syntax

  socket.emit('event_name',{"data"}); 
+2
source share

All Articles