byzz
byzz

Reputation: 360

socket.io How Can I Get "Socket object" list in room

folks. So far, I have been searching "How can I get socket list in room". I was able to find a lot of results.

like this link. How to get room's clients list in socket.io 1.0 example, console.log(io.nsps['/'].adapter.rooms);

But, That answers can get the socket list in room not "Object".

I need socket object list of that into the socket in the room.

How can I get socket's object list in rooms. Please tell me solution.

Upvotes: 0

Views: 1406

Answers (1)

Christopher Reid
Christopher Reid

Reputation: 4484

io.sockets.connected[SOCKET_ID_GOES_HERE]

You can use io.nsps['/'].adapter.rooms to find socket ids and then get the objects you need with the following code:

var sockets_in_room = io.nsps['/'].adapter.rooms[ROOM_NAME_GOES_HERE]
var socket_objects = []

for (socketId in sockets_in_room) {
    socket_objects.push(io.sockets.connected[socketId])
}

Upvotes: 4

Related Questions