Skip to content

Commit f299c64

Browse files
docs: add note about the "disconnecting" event
Related: socketio/socket.io#1814
1 parent 3c99038 commit f299c64

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

docs/server-api.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,8 @@ Possible reasons:
15571557

15581558
#### Event: 'disconnecting'
15591559

1560+
*Added in v1.5.0*
1561+
15601562
- `reason` [`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) the reason of the disconnection (either client or server-side)
15611563

15621564
Fired when the client is going to be disconnected (but hasn't left its `rooms` yet).
@@ -1569,13 +1571,32 @@ io.on("connection", (socket) => {
15691571
});
15701572
```
15711573

1572-
Note: those events, along with `connect`, `connect_error`, `newListener` and `removeListener`, are special events that shouldn't be used in your application:
1574+
With an asynchronous handler, you will need to create a copy of the `rooms` attribute:
1575+
1576+
```js
1577+
io.on("connection", (socket) => {
1578+
socket.on("disconnecting", async (reason) => {
1579+
const rooms = new Set(socket.rooms);
1580+
1581+
await someLongRunningOperation();
1582+
1583+
// socket.rooms will be empty there
1584+
console.log(rooms);
1585+
});
1586+
});
1587+
```
1588+
1589+
:::caution
1590+
1591+
Those events, along with `connect`, `connect_error`, `newListener` and `removeListener`, are special events that shouldn't be used in your application:
15731592

15741593
```js
15751594
// BAD, will throw an error
15761595
socket.emit("disconnect");
15771596
```
15781597

1598+
:::
1599+
15791600
### Attributes
15801601

15811602
#### socket.client

0 commit comments

Comments
 (0)