Skip to content

Commit 1898586

Browse files
feat: allow to modify the Redis key for the session
Related: #9
1 parent 8ff0413 commit 1898586

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ io.listen(3000);
5656

5757
## Options
5858

59-
| Name | Description | Default value |
60-
|---------------------|--------------------------------------------------------------------|---------------|
61-
| `streamName` | The name of the Redis stream. | `socket.io` |
62-
| `maxLen` | The maximum size of the stream. Almost exact trimming (~) is used. | `10_000` |
63-
| `readCount` | The number of elements to fetch per XREAD call. | `100` |
64-
| `heartbeatInterval` | The number of ms between two heartbeats. | `5_000` |
65-
| `heartbeatTimeout` | The number of ms without heartbeat before we consider a node down. | `10_000` |
59+
| Name | Description | Default value |
60+
|---------------------|-------------------------------------------------------------------------------------------------------------------|----------------|
61+
| `streamName` | The name of the Redis stream. | `socket.io` |
62+
| `maxLen` | The maximum size of the stream. Almost exact trimming (~) is used. | `10_000` |
63+
| `readCount` | The number of elements to fetch per XREAD call. | `100` |
64+
| `sessionKeyPrefix` | The prefix of the key used to store the Socket.IO session, when the connection state recovery feature is enabled. | `sio:session:` |
65+
| `heartbeatInterval` | The number of ms between two heartbeats. | `5_000` |
66+
| `heartbeatTimeout` | The number of ms without heartbeat before we consider a node down. | `10_000` |
6667

6768
## How it works
6869

lib/adapter.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface ClusterAdapterOptions {
3131
export interface RedisStreamsAdapterOptions {
3232
/**
3333
* The name of the Redis stream.
34+
* @default "socket.io"
3435
*/
3536
streamName?: string;
3637
/**
@@ -43,6 +44,11 @@ export interface RedisStreamsAdapterOptions {
4344
* @default 100
4445
*/
4546
readCount?: number;
47+
/**
48+
* The prefix of the key used to store the Socket.IO session, when the connection state recovery feature is enabled.
49+
* @default "sio:session:"
50+
*/
51+
sessionKeyPrefix?: string;
4652
}
4753

4854
interface RawClusterMessage {
@@ -68,6 +74,7 @@ export function createAdapter(
6874
streamName: "socket.io",
6975
maxLen: 10_000,
7076
readCount: 100,
77+
sessionKeyPrefix: "sio:session:",
7178
heartbeatInterval: 5_000,
7279
heartbeatTimeout: 10_000,
7380
},
@@ -239,9 +246,10 @@ class RedisStreamsAdapter extends ClusterAdapterWithHeartbeat {
239246

240247
override persistSession(session) {
241248
debug("persisting session %o", session);
249+
const sessionKey = this.#opts.sessionKeyPrefix + session.pid;
242250
const encodedSession = Buffer.from(encode(session)).toString("base64");
243251

244-
this.#redisClient.set(`sio:session:${session.pid}`, encodedSession, {
252+
this.#redisClient.set(sessionKey, encodedSession, {
245253
PX: this.nsp.server.opts.connectionStateRecovery.maxDisconnectionDuration,
246254
});
247255
}
@@ -256,7 +264,7 @@ class RedisStreamsAdapter extends ClusterAdapterWithHeartbeat {
256264
return Promise.reject("invalid offset");
257265
}
258266

259-
const sessionKey = `sio:session:${pid}`;
267+
const sessionKey = this.#opts.sessionKeyPrefix + pid;
260268

261269
const [rawSession, offsetExists] = await this.#redisClient
262270
.multi()

0 commit comments

Comments
 (0)