Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io get rooms which socket is currently in

Tags:

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

like image 546
Herokiller Avatar asked Sep 27 '13 07:09

Herokiller


People also ask

How many rooms can Socket.IO have?

socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.


2 Answers

In socket.io version 1+ the syntax is:

socket.rooms 
like image 90
Arjen Avatar answered Sep 27 '22 20:09

Arjen


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'] 
like image 29
Alexandre Daubricourt Avatar answered Sep 27 '22 19:09

Alexandre Daubricourt