How to reconnect socket.io client on activity after auto-reconnect times out?

javascript, socket.io

Solution

In version 0.9 you could try to set the connect options object to some aggressive settings:

  var main = io.connect('/', {
    'reconnection delay': 100, // defaults to 500
    'reconnection limit': 100, // defaults to Infinity
    'max reconnection attempts': Infinity // defaults to 10
  });

Note that `max reconnection attemps` does not mean that the io client will stop to reconnect to the server after 10 failed attempts. If it was able to reconnect to the server 10 times and loses the connection for the 11th time, it will stop to reconnect.

See Socket.IO Configuration

Problem

It is common for laptops to go to sleep. This causes the socket.io client to disconnect. When the user returns to the web app, the socket.io client doesn't try to reconnect (probably reconnection limit reached?). How do I tell the socket to reconnect if the user does some action? For example, I'd like the `emit` function to check if the connection is active, and if not then try to reconnect. Note: I only need the client-side JS code and I'm not using node.js.

Original source