"connection" event fired on every client heartbeat?


I have a basic server setup like this:

var server = express.createServer().listen(port); = socketIO.listen(server);'connection', function(socket){

I also have configured to use XHR polling like so:

io.set("transports", ["xhr-polling"]);
io.set("close timeout", 3);
io.set("heartbeat interval", 2); 

The issue is every time the client sends a heartbeat (every 2 sec), the 'connection' event is being fired. Is there a different event I can use that will fire once each time a socket initially connects?

I would use the "authorization" event, but that only passes in a handshake object not the actual socket.

Problem courtesy of: levi


Found the problem. I had my xhr "polling duration" set to heroku's suggested 10s like so:

io.set("polling duration", 10);

Which means that the client only makes a new xhr request every 10 seconds (as soon as the previous request returns). At the same time I had the "close timeout" set to 3 seconds:

io.set("close timeout", 3);

Which means if the server does not hear from the client within 3 seconds since its last request it closes the connection, hence the continuous 'disconnect/connection' events being fired.

The solution was to set the close timeout higher than the polling duration. It would seem that the "heartbeat interval" is not relevant for xhr connections.

Solution courtesy of: levi


Are you setting too low a value for closetimeout and heartbeat interval. The default values for these are 60 sec and 25 sec. Typical network lag is of the order of 5 sec. So with the values that you have set, the client does not respond in the time specified, so the connection is closed. When the client connects again, the connection event is fired.

Discussion courtesy of: almypal

This recipe can be found in it's original form on Stack Over Flow.