Skip to content

Feature: disconnect all sockets of a room. #3570

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

Closed
royalpinto opened this issue Apr 1, 2020 · 1 comment
Closed

Feature: disconnect all sockets of a room. #3570

royalpinto opened this issue Apr 1, 2020 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@royalpinto
Copy link

Feature request:

I would like to request a feature to disconnect all sockets of a given room.

Use case:

A logged-in user has multiple connections to the server (and are also connected to different node processes). Upon user logout or session expiry, I would like to disconnect all the client connections of that user session. So, I have created a unique room for each user session id and joined all the connections to that room. So I can simply disconnect all the clients connected to that room (i.e to that session).

Expecting something like this:

io.in('room-name').disconnect(true)

This is missing as of now in the lib. Also, it should send signals over the adaptor to other processes to disconnect from all the other processes too.

Any suggestions? If it's a good approach, I can create a PR for it.

@darrachequesne darrachequesne added the enhancement New feature or request label Feb 23, 2021
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 in b25495c and included in [email protected]

Syntax:

// 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();

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
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
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants