I want to statically remove all users from a room, effectively deleting that room. The idea is that another room with the same name may be created again in the future, but I want it created empty (without the listeners from the previous room).
I'm not interested in managing the room status myself but rather curious as if I can leverage socket.io internals to do this. Is this possible? (see also this question)
Is that what you want ?
io.sockets.clients(someRoom).forEach(function(s){
s.leave(someRoom);
});
For an up-to-date answer to this question, everyone who wants to remove a room can make use of Namespace.clients(cb)
. The cb
callback will receive an error object as the first argument (null
if no error) and a list of socket IDs as the second argument.
It should work fine with socket.io
v2.1.0
, not sure which version is the earliest compatible one.
io.of('/').in('chat').clients((error, socketIds) => {
if (error) throw error;
socketIds.forEach(socketId => io.sockets.sockets[socketId].leave('chat'));
});
@See https://github.com/socketio/socket.io/issues/3042
@See https://socket.io/docs/server-api/#namespace-clients-callback