xinaris
xinaris

Reputation: 464

Node.js - Socket.io - Issue with "maximum concurrent connections"

I have a problem with a socket.io nodejs app (in Windows Azure). It works fine but after a while I am getting an error where the server replies back as:

HTTP/1.1 503 Number of active WebSocket requests has reached the maximum concurrent WebSocket requests allowed

Server setup:

var client_id = 0;
var connectCounter = 0;

var server = http.createServer(function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  console.log('server init.');
}).listen(port);
io = io.listen(server);

io.sockets.on('connection', function(socket)
{
    socket.client_id = client_id;
    client_id++;
    connectCounter++;
    console.log('A socket with client_id ' + socket.client_id + ' connected!');
    console.log('Sockets counter: ' + connectCounter);

    socket.on('disconnect', function() {
        console.log('A socket with client_id ' + socket.client_id + ' disconnected!');
        connectCounter--;
        console.log('Sockets counter: ' + connectCounter);
    });
});

Example log at the end:

A socket with client_id 349 connected!
Sockets counter: 1 
A socket with client_id 350 connected!
Sockets counter: 2 
A socket with client_id 349 disconnected! 
Sockets counter: 1 
A socket with client_id 350 disconnected! 
Sockets counter: 0

Since my sockets are connecting and disconnecting, there shouldn't be 350 concurrent connections. What I am doing wrong?

Upvotes: 2

Views: 1598

Answers (1)

jonny
jonny

Reputation: 3098

As per my comment, the issue may well reside in a bug lying in the engine.io module, which is a component of socket.io. Ostensibly, the cause is that engine.io doesn't close a tcp socket when detecting ping timeout.

In a GitHub issue I found, they apparently patched the bug by adding a self.transport.close() call to the file located at the path:

node_modules/socket.io/node_modules/engine.io/lib/socket.js

Which should be somewhere in your node app directory.

I found the line of code at this GitHub pull. The code is as follows:

Socket.prototype.setPingTimeout = function () {
    var self = this;
    clearTimeout(self.pingTimeoutTimer);
    self.pingTimeoutTimer = setTimeout(function () {
        self.onClose('ping timeout');
        self.transport.close(); // This is the added line
    }, self.server.pingInterval + self.server.pingTimeout);
};

Try that out, it might solve your issue.

Upvotes: 2

Related Questions