4

I'm trying to implement socket.io with Redis adapter in NodeJs.

Mostly it works, but sometimes I am still getting errors when trying to disconnect / connect sockets, so I think I haven't implement it correctly.

Could someone please explain what is the difference between socket.disconnect(); and io.of('/').adapter.remoteDisconnect();

If I initialise my io with:

io.adapter(redisIO({
   host: config.server.redis.host,
   port: config.server.redis.port,
   requestsTimeout: config.server.redis.request_timeout
}));

Shouldn't then socket.disconnect(); be aware of using redisIO? If using remoteDisconnect can I still capture socket.on('disconnect', fn) or should remoteDisconnect be called in socket.on('disconnect', fn)?

What happens if client disconnects? How can I propagate it to socket.io cluster?

Any working examples will be appreciated :)

Thanks!

Miha Trtnik
  • 236
  • 1
  • 3
  • 8

1 Answers1

0

Capture the socket event disconnecting, not disconnect, by then it's too late!

socket.on('disconnecting', () => {
  var room_name = sess.id
  io.of('/').adapter.remoteDisconnect(room_name, true, err => {
     delete socket.rooms[room_name]
     socket.removeAllListeners()
  })
})

buycanna.io
  • 1,166
  • 16
  • 18