Skip to content

Commit

Permalink
docs: improve documentation about disconnection
Browse files Browse the repository at this point in the history
There are two cases (and not one) where the client will not try to
reconnect automatically.

Related: socketio/socket.io-client#1443
  • Loading branch information
darrachequesne committed Feb 13, 2021
1 parent 88158a8 commit 644c69d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion source/docs/v3/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,9 @@ Reason | Description
`transport close` | The connection was closed (example: the user has lost connection, or the network was changed from WiFi to 4G)
`transport error` | The connection has encountered an error (example: the server was killed during a HTTP long-polling cycle)

In all cases but the first (disconnection by the server), the client will wait for a small random delay and then reconnect.
In the first two cases (explicit disconnection), the client will not try to reconnect and you need to manually call `socket.connect()`.

In all other cases, the client will wait for a small [random delay](/docs/v3/client-initialization/#reconnectionDelay) and then try to reconnect:

```js
socket.on('disconnect', (reason) => {
Expand Down
14 changes: 14 additions & 0 deletions source/docs/v3/client-socket-instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ Reason | Description
`transport close` | The connection was closed (example: the user has lost connection, or the network was changed from WiFi to 4G)
`transport error` | The connection has encountered an error (example: the server was killed during a HTTP long-polling cycle)

In the first two cases (explicit disconnection), the client will not try to reconnect and you need to manually call `socket.connect()`.

In all other cases, the client will wait for a small [random delay](/docs/v3/client-initialization/#reconnectionDelay) and then try to reconnect:

```js
socket.on("disconnect", (reason) => {
if (reason === "io server disconnect") {
// the disconnection was initiated by the server, you need to reconnect manually
socket.connect();
}
// else the socket will automatically try to reconnect
});
```

Note: those events, along with `disconnecting`, `newListener` and `removeListener`, are special events that shouldn't be used in your application:

```js
Expand Down
5 changes: 3 additions & 2 deletions source/docs/v3/server-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,9 @@ Possible reasons:

Reason | Description
------ | -----------
`io server disconnect` | The socket was forcefully disconnected with [socket.disconnect()](/docs/v3/server-api/#socket-disconnect-close)
`io client disconnect` | The client has manually disconnected the socket using [socket.disconnect()](/docs/v3/client-api/#socket-disconnect)
`server namespace disconnect` | The socket was forcefully disconnected with [socket.disconnect()](/docs/v3/server-api/#socket-disconnect-close)
`client namespace disconnect` | The client has manually disconnected the socket using [socket.disconnect()](/docs/v3/client-api/#socket-disconnect)
`server shutting down` | The server is, well, shutting down
`ping timeout` | The client did not respond in the `pingTimeout` range
`transport close` | The connection was closed (example: the user has lost connection, or the network was changed from WiFi to 4G)
`transport error` | The connection has encountered an error
Expand Down

0 comments on commit 644c69d

Please sign in to comment.