Skip to content
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

Closed
wendux opened this issue Aug 29, 2017 · 9 comments
Closed

how to clear the room ? #3042

wendux opened this issue Aug 29, 2017 · 9 comments
Milestone

Comments

@wendux
Copy link

wendux commented Aug 29, 2017

like io.to("chat").clear() , all socket in room "chat" will leave

@rllalloshi
Copy link

rllalloshi commented Sep 30, 2017

@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.
You can do this to remove all clients from a room:

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

  socketIds.forEach(socketId => io.sockets.sockets[socketId].leave('chat'));

});

https://socket.io/docs/server-api/#namespace-clients-callback

@darrachequesne
Copy link
Member

To complete @rllalloshi's answer, in a multi-server configuration you should use the removeLeave method of the adapter:

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

  socketIds.forEach(socketId => io.of('/').adapter.remoteLeave(socketId, 'chat'));

});

Reference

@mirajehossain
Copy link

@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.
You can do this to remove all clients from a room:

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

  socketIds.forEach(socketId => io.sockets.sockets[socketId].leave('chat'));

});

https://socket.io/docs/server-api/#namespace-clients-callback

it did not work in socket.io 2.2.0

@mewsu
Copy link

mewsu commented Nov 29, 2019

Why is there no simple call to remove all sockets from a room? It is a very common use case

@p3nGu1nZz
Copy link

i vote to add. room.kick() function where you can pass in a specific id, an array, or 'ALL" for removing everyone

@Rahulm2310
Copy link

Rahulm2310 commented Jan 22, 2021

@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.
You can do this to remove all clients from a room:

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

  socketIds.forEach(socketId => io.sockets.sockets[socketId].leave('chat'));

});

https://socket.io/docs/server-api/#namespace-clients-callback

@rllalloshi didn't worked me
TypeError: io.of(...).in(...).clients is not a function

darrachequesne added a commit that referenced this issue Mar 2, 2021
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
@darrachequesne
Copy link
Member

For future readers: this was implemented by b25495c and included in [email protected].

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

@darrachequesne darrachequesne added this to the 4.0.0 milestone Mar 11, 2021
darrachequesne added a commit to socketio/socket.io-website that referenced this issue Jun 28, 2021
@darrachequesne
Copy link
Member

@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 👍

dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants
@mewsu @rllalloshi @darrachequesne @p3nGu1nZz @mirajehossain @wendux @Rahulm2310 and others