-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to clear the room ? #3042
Comments
@wendux rooms are created and deleted automatically. That means when the room is empty, there are no more clients left on it, it will be deleted.
https://socket.io/docs/server-api/#namespace-clients-callback |
To complete @rllalloshi's answer, in a multi-server configuration you should use the io.of('/').in('chat').clients((error, socketIds) => {
if (error) throw error;
socketIds.forEach(socketId => io.of('/').adapter.remoteLeave(socketId, 'chat'));
}); |
it did not work in socket.io 2.2.0 |
Why is there no simple call to remove all sockets from a room? It is a very common use case |
i vote to add. room.kick() function where you can pass in a specific id, an array, or 'ALL" for removing everyone |
@rllalloshi didn't worked me |
This commit adds the following methods: - fetchSockets: returns the matching socket instances Syntax: ```js // return all Socket instances const sockets = await io.fetchSockets(); // return all Socket instances of the "admin" namespace in the "room1" room const sockets = await io.of("/admin").in("room1").fetchSockets(); ``` - socketsJoin: makes the matching socket instances join the specified rooms Syntax: ```js // make all Socket instances join the "room1" room io.socketsJoin("room1"); // make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room io.of("/admin").in("room1").socketsJoin("room2"); ``` - socketsLeave: makes the matching socket instances leave the specified rooms Syntax: ```js // make all Socket instances leave the "room1" room io.socketsLeave("room1"); // make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room io.of("/admin").in("room1").socketsLeave("room2"); ``` - disconnectSockets: makes the matching socket instances disconnect Syntax: ```js // make all Socket instances disconnect io.disconnectSockets(); // make all Socket instances of the "admin" namespace in the "room1" room disconnect io.of("/admin").in("room1").disconnectSockets(); ``` Those methods share the same semantics as broadcasting. They will also work with multiple Socket.IO servers when using the Redis adapter. In that case, the fetchSockets() method will return a list of RemoteSocket instances, which expose a subset of the methods and attributes of the Socket class (the "request" attribute cannot be mocked, for example). Related: - #3042 - #3418 - #3570 - socketio/socket.io-redis-adapter#283
For future readers: this was implemented by b25495c and included in Syntax: // make all Socket instances leave the "room1" room
io.socketsLeave("room1");
// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
io.of("/admin").in("room1").socketsLeave("room2"); Documentation: https://socket.io/docs/v3/migrating-from-3-x-to-4-0/#Additional-utility-methods |
@taylordwright44 since the socket joins the room identified by its own ID, you can use: io.in(theSocketId).socketsLeave("room1"); I've added this example in the documentation. Thanks 👍 |
This commit adds the following methods: - fetchSockets: returns the matching socket instances Syntax: ```js // return all Socket instances const sockets = await io.fetchSockets(); // return all Socket instances of the "admin" namespace in the "room1" room const sockets = await io.of("/admin").in("room1").fetchSockets(); ``` - socketsJoin: makes the matching socket instances join the specified rooms Syntax: ```js // make all Socket instances join the "room1" room io.socketsJoin("room1"); // make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room io.of("/admin").in("room1").socketsJoin("room2"); ``` - socketsLeave: makes the matching socket instances leave the specified rooms Syntax: ```js // make all Socket instances leave the "room1" room io.socketsLeave("room1"); // make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room io.of("/admin").in("room1").socketsLeave("room2"); ``` - disconnectSockets: makes the matching socket instances disconnect Syntax: ```js // make all Socket instances disconnect io.disconnectSockets(); // make all Socket instances of the "admin" namespace in the "room1" room disconnect io.of("/admin").in("room1").disconnectSockets(); ``` Those methods share the same semantics as broadcasting. They will also work with multiple Socket.IO servers when using the Redis adapter. In that case, the fetchSockets() method will return a list of RemoteSocket instances, which expose a subset of the methods and attributes of the Socket class (the "request" attribute cannot be mocked, for example). Related: - socketio#3042 - socketio#3418 - socketio#3570 - socketio/socket.io-redis-adapter#283
like io.to("chat").clear() , all socket in room "chat" will leave
The text was updated successfully, but these errors were encountered: