Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 596
Josh1billion Avatar asked Mar 05 '12 09:03

Josh1billion


People also ask

How many messages per second can Socket.IO handle?

Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok. I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.

How many concurrent connections can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

Does Socket.IO use a lot of memory?

Basic usage of socket.io causes incremental memory usage (about +4mb every second). Even when nothing is transmitting.


1 Answers

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

like image 195
CoderMD666 Avatar answered Oct 26 '22 12:10

CoderMD666