socket.io client reconnect timeout

There is a configuration option, reconnection limit (see Configuring Socket.IO):

reconnection limit defaults to Infinity

  • The maximum reconnection delay in milliseconds, or Infinity.

It can be set as follows:

io.set("reconnection limit", 5000);

When set, the reconnection delay will continue to increase (according to the exponential back off algorithm), but only up to the maximum value you specify.


EDIT: See answer below for proper approach

I am afraid that the reconnection algorithm cannot be modified (as of December 2013); the Github issue to allow this feature is not merged yet. However, one of the commentors is suggesting a small workaround, which should nullify the exponential increase:

socket.socket.reconnectionDelay /= 2 on reconnecting

The other approach is, as you said, to write some client-side code to overwrite the reconnecting behavior, and do polling. Here is an example of how this could be done.

EDIT: the above code will have to go inside the 'disconnect' event callback:

var socket = io('http://localhost');
socket.on('connect', function(){
  socket.on('disconnect', function(){
      socket.socket.reconnectionDelay /= 2;
  });
});

This is the solution I went with - when the socket disconnects it enters a loop that keeps trying to reconnect every 3 seconds until the connection is created - works a treat:

  socket.on('disconnect', function() {
    socketConnectTimeInterval = setInterval(function () {
      socket.socket.reconnect();
      if(socket.socket.connected) {clearInterval(socketConnectTimeInterval);}
    }, 3000);
  });

cheers