mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
Connection state recovery allows a client to reconnect after a
temporary disconnection and restore its state:
- id
- rooms
- data
- missed packets
Usage:
```js
import { Server } from "socket.io";
const io = new Server({
connectionStateRecovery: {
// default values
maxDisconnectionDuration: 2 * 60 * 1000,
skipMiddlewares: true,
},
});
io.on("connection", (socket) => {
console.log(socket.recovered); // whether the state was recovered or not
});
```
Here's how it works:
- the server sends a session ID during the handshake (which is
different from the current `id` attribute, which is public and can be
freely shared)
- the server also includes an offset in each packet (added at the end
of the data array, for backward compatibility)
- upon temporary disconnection, the server stores the client state for
a given delay (implemented at the adapter level)
- upon reconnection, the client sends both the session ID and the last
offset it has processed, and the server tries to restore the state
A few notes:
- the base adapter exposes two additional methods, persistSession() and
restoreSession(), that must be implemented by the other adapters in
order to allow the feature to work within a cluster
See: f5294126a8
- acknowledgements are not affected, because it won't work if the
client reconnects on another server (as the ack id is local)
- any disconnection that lasts longer than the
`maxDisconnectionDuration` value will result in a new session, so users
will still need to care for the state reconciliation between the server
and the client
Related: https://github.com/socketio/socket.io/discussions/4510
25 lines
652 B
TypeScript
25 lines
652 B
TypeScript
"use strict";
|
|
|
|
import expect from "expect.js";
|
|
|
|
describe("socket.io", () => {
|
|
it("should be the same version as client", () => {
|
|
const version = require("../package").version;
|
|
expect(version).to.be(require("socket.io-client/package.json").version);
|
|
});
|
|
|
|
require("./server-attachment");
|
|
require("./handshake");
|
|
require("./close");
|
|
require("./namespaces");
|
|
require("./socket");
|
|
require("./messaging-many");
|
|
require("./middleware");
|
|
require("./socket-middleware");
|
|
require("./v2-compatibility");
|
|
require("./socket-timeout");
|
|
require("./uws");
|
|
require("./utility-methods");
|
|
require("./connection-state-recovery");
|
|
});
|