socket.io do sockets leave rooms on disconnect code example

Example: socket.io do sockets leave rooms on disconnect

/* Upon disconnection, sockets leave all the channels they were part of 
automatically, and no special teardown is needed on your part.

You can fetch the rooms the Socket was in by listening to the 
disconnecting event: */

io.on('connection', socket => {
  socket.on('disconnecting', () => {
    console.log(socket.rooms); // the Set contains at least the socket ID
  });

  socket.on('disconnect', () => {
    // socket.rooms.size === 0
  });
});

Tags:

Misc Example