28

Is it possible to get rooms which socket is currently in, without calling

io.sockets.clients(roomName)

for every room name and looking for this socket in results

Herokiller
  • 2,891
  • 5
  • 32
  • 50

12 Answers12

44

In socket.io version 1+ the syntax is:

socket.rooms
Arjen
  • 451
  • 1
  • 4
  • 3
  • 4
    `socket.rooms` showing up blank in 1.3.6 ... does this still work for you? – Crashalot Aug 20 '15 at 06:35
  • 3
    Returns `undefined`, version: 1.6.0. – Mr. B. Nov 30 '16 at 11:30
  • 2
    for me, it returns a blank object `{}`, I'm using 2.3.0 – Dylan Mar 03 '20 at 22:12
  • I am using 2.3.0 too, but it seems that it works for me? Even if no `socket.join()` is called it would returned a channel that equals to the socket id. Maybe you are not calling this inside the connection function? Or your connection function is having the parameter other then `socket`? – cytsunny Jul 24 '20 at 10:01
13

Cross-compatible way

var rooms = Object.keys(io.sockets.adapter.sids[socket.id]);
// returns [socket.id, 'room-x'] or [socket.id, 'room-1', 'room-2', ..., 'room-x']
Alexandre Daubricourt
  • 3,323
  • 1
  • 34
  • 33
13

Update: Socket.io 3.0 Released


With 3.x Socket.rooms is Set now, which means that values in the rooms may only occur once.

Structure example: Set(4) {"<socket ID>", "room1", "room2", "room3"}

io.on("connect", (socket) => {
  console.log(socket.rooms); // Set { <socket.id> }
  socket.join("room1");
  console.log(socket.rooms); // Set { <socket.id>, "room1" }
});

To check for specific room:

socket.rooms.has("room1"); // true

More about Set and available methods: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

Migration Docs: https://socket.io/docs/migrating-from-2-x-to-3-0/

t_dom93
  • 10,226
  • 1
  • 52
  • 38
12

From the Socket.IO Room doc:

io.sockets.manager.roomClients[socket.id]

Laurent Perrin
  • 14,671
  • 5
  • 50
  • 49
4

When using a non-default adapter, such as socket.io-redis, socket.rooms doesn't seem to do the trick. The way I managed to get the rooms for a specific client without looping was to use io.sockets.adapter.sids[socket.id], which returns the rooms as an object.

{ 'R-ZRgSf7h4wfPatcAAAC': true, ROOM: true, ROOM_2: true }

Note that this doesn't list sockets on other processes, though!

socket.io v1.3.7, socket.io-redis 1.0.0

natancodes
  • 998
  • 8
  • 12
2

Version 1.7.3, socket.rooms contains socket.id, so remove it and get the list of rooms:

Object.keys(socket.rooms).filter(item => item!=socket.id);

In other version, you can print the socket and find the rooms.

Zing Lee
  • 720
  • 6
  • 10
2

Being sure that socket is in only one room at a time, my solution was:

var currentRoom = Object.keys(io.sockets.adapter.sids[socket.id]).filter(item => item!=socket.id);
Adry
  • 307
  • 1
  • 4
  • 21
2

Socket.io v2.1.1

So make sure you aren't accessing the sockets rooms in the disconnect event like I was, as they have already left the rooms by the time that event is triggered. If you want to do that try it in the disconnecting event - https://github.com/socketio/socket.io/pull/2332/files

Then you can use any of the following:

Object.keys(socket.adapter.rooms)
Object.keys(socket.adapter.sids)
Object.keys(socket.rooms)
  • Using this method, is there a way to look up rooms that a socket is in by a `socket.id` string? ie not using the `socket` that is passed to an event, but just a string containing `socket.id`? – user1063287 Nov 27 '18 at 13:36
1

Version 2.0.3

io.sockets.sockets[yourSocketID].rooms

That equal with

socket.rooms
StefansArya
  • 2,802
  • 3
  • 24
  • 25
0

socket.io 1.7.3 +

var currentRoom = socket.rooms[Object.keys(socket.rooms)[0]];//returns name of room
TwistedOwl
  • 1,195
  • 1
  • 17
  • 30
0

You can save room in socket itself when it joins a room

// join room
socket.join(room);

// update socket's rooms
if (socket.rooms) {
    socket.rooms.push(room);
} else {
    socket.rooms = [room];
}

Later you can retrieve all rooms that the socket is in by simply

socket.rooms

From the Server API documentation:

socket.rooms (object)
A hash of strings identifying the rooms this client is in, indexed by room name.

https://socket.io/docs/server-api/#socket-rooms

user1063287
  • 10,265
  • 25
  • 122
  • 218
pradeep
  • 550
  • 4
  • 10
  • 2
    Just wondering, wouldn't `socket.rooms` be updated automatically by `socket.io` every time a socket joined a room? – user1063287 Nov 27 '18 at 13:46
  • in no way should you be modifying the API like this. why would you need to update socket.rooms when the library already does this? – Emobe Feb 28 '19 at 19:23
0

With socket.rooms you will get a set of socketId and its rooms.

So you can convert it into array and then slice it to get only the rooms like this:

[...socket.rooms].slice(1, );

And then we can iterate through that array or access any room, for example:

[...socket.rooms].slice(1, )[1] // to get first room
ChristianM
  • 1,793
  • 12
  • 23