Salvador Dali
Salvador Dali

Reputation: 222889

Socket.io find rooms the client has joined

Is there a way to find what rooms a client joined?

The situation I am trying to solve is the following: people can join different rooms, but I want to notify people in the room if the person leaves. I know that I have to use on disconnect event and emit the message to all people in that room, but I can not find a way to know what rooms the person is in.

io.sockets.on('connection', function(client) {
   client.on('disconnect', function() {
      // ...
   });
});

Upvotes: 0

Views: 782

Answers (1)

Oleg
Oleg

Reputation: 23307

client.rooms would do the trick.

Note: Keep in mind that according to the docs,

For your convenience, each socket automatically joins a room identified by this id.

So by default each socket is joined to at least one room.

Upvotes: 1

Related Questions