'how to get socket.io number of clients in room?
my socket.io version 1.3.5
I want to get number of clients in particular room.
This is my code.
socket.on('create or join', function (numClients, room) {
socket.join(room);
});
I use this code for get clients in room :
console.log('Number of clients',io.sockets.clients(room));
Solution 1:[1]
This works for version 3
io.sockets.adapter.rooms.get(roomName).size
Solution 2:[2]
To get the number of clients in a room you can do the following:
function NumClientsInRoom(namespace, room) {
var clients = io.nsps[namespace].adapter.rooms[room];
return Object.keys(clients).length;
}
This variable clients will hold a object where each client is a key. Then you just get the number of clients (keys) in that object.
If you haven't defined a namespace the default one is "/".
Solution 3:[3]
Have a counter variable to keep count, increase when someone joins and decrease when people disconnect.
io.on('connection', function (socket) {
var numClients = {};
socket.on('join', function (room) {
socket.join(room);
socket.room = room;
if (numClients[room] == undefined) {
numClients[room] = 1;
} else {
numClients[room]++;
}
});
socket.on('disconnect', function () {
numClients[socket.room]--;
});
Solution 4:[4]
This work for me.
// check if your room isn't undefined.
if (io.sockets.adapter.rooms['your room name'])
{
// result
console.log(io.sockets.adapter.rooms['your room name'].length);
}
Solution 5:[5]
this work for me
io.in('yourRoom').allSockets().then(result=>{
console.log(res.size) })
Solution 6:[6]
In socket.io ^1.4.6
function numClientsInRoom(namespace, room) {
var clients = io.nsps[namespace].adapter.rooms[room].sockets;
return Object.keys(clients).length;
}
You need to add .sockets to rooms[room], because variable clients holds all connection inside sockets variable.
Solution 7:[7]
i'm a bit late to the party, but I found a valid solution:
io.in('YOUR_ROOM').engine.clientsCount;
Solution 8:[8]
socket.io's rooms is a Map whose elements map to Sets.
You use get(roomId) to get the value of that roomId in a Map and you use .size to the number of elements in a set unlike .length for number of elements in an array.
socket.on("join-room",roomId => {
const room = io.of("/yourNameSpace").adapter.rooms.get(roomId)
if(room === undefined || room.size < 2) {
socket.join(roomId)
}else {
io.of("/yourNameSpace").emit("can't join link")
}
})
Solution 9:[9]
io.nsps['/'].adapter.rooms['your room name'].sockets;
If your namespace is not /, change it.
Solution 10:[10]
Only one that worked for me in 2022 using socket.io 4.5.1:
io._nsps.get('/').adapter.rooms.get('your-room-id').size;
Thanks to @Abhishek Kumar, just making a top level comment for visibility.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Parag Jain |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | jalal |
| Solution 6 | Woolfi182 |
| Solution 7 | Dharman |
| Solution 8 | user3840170 |
| Solution 9 | Mario Varchmin |
| Solution 10 | Joe Roddy |
