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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With