Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of clients in a socket.io room (2.0)

Tags:

socket.io

How can you get a list of all clients in a room when using Socket.io 2.0?

There are a lot of related questions, but none are for version 2.0 or answer this question. The closest answer is for 2.0, but only explains how to get a list of clients when using Redis, which is not a requirement for using socket.io.

like image 438
Don P Avatar asked Oct 02 '17 19:10

Don P


1 Answers

Found it, the answer was buried in Socket.io's docs under "namespace", not "room".

For example, if you are in the namespace "/chat" and want all clients in the room "general", you can do this:

io.of('/chat').in('general').clients((error, clients) => {
  if (error) throw error;

  // Returns an array of client IDs like ["Anw2LatarvGVVXEIAAAD"]
  console.log(clients); 
});
like image 68
Don P Avatar answered Nov 09 '22 09:11

Don P