Node.js and Socket.IO – How to reconnect as soon as disconnect happens

EDIT: socket.io now has built-in reconnection support. Use that.

e.g. (these are the defaults):

io.connect('http://localhost', {
  'reconnection': true,
  'reconnectionDelay': 500,
  'reconnectionAttempts': 10
});

This is what I did:

socket.on('disconnect', function () {
  console.log('reconnecting...')
  socket.connect()
})
socket.on('connect_failed', function () {
  console.log('connection failed. reconnecting...')
  socket.connect()
})

It seems to work pretty well, though I’ve only tested it on the websocket transport.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)