@socket.io/redis-adapter
Advanced tools
+3
-0
@@ -55,4 +55,6 @@ import { Adapter, BroadcastOptions, Room } from "socket.io-adapter"; | ||
| private readonly responseChannel; | ||
| private readonly specificResponseChannel; | ||
| private requests; | ||
| private ackRequests; | ||
| private redisListeners; | ||
| /** | ||
@@ -124,3 +126,4 @@ * Adapter constructor. | ||
| serverCount(): Promise<number>; | ||
| close(): Promise<void> | void; | ||
| } | ||
| export {}; |
+38
-8
@@ -69,2 +69,3 @@ "use strict"; | ||
| this.ackRequests = new Map(); | ||
| this.redisListeners = new Map(); | ||
| this.uid = uid2(6); | ||
@@ -78,21 +79,29 @@ this.requestsTimeout = opts.requestsTimeout || 5000; | ||
| this.responseChannel = prefix + "-response#" + this.nsp.name + "#"; | ||
| const specificResponseChannel = this.responseChannel + this.uid + "#"; | ||
| this.specificResponseChannel = this.responseChannel + this.uid + "#"; | ||
| const isRedisV4 = typeof this.pubClient.pSubscribe === "function"; | ||
| if (isRedisV4) { | ||
| this.subClient.pSubscribe(this.channel + "*", (msg, channel) => { | ||
| this.redisListeners.set("psub", (msg, channel) => { | ||
| this.onmessage(null, channel, msg); | ||
| }, true); | ||
| this.subClient.subscribe([this.requestChannel, this.responseChannel, specificResponseChannel], (msg, channel) => { | ||
| }); | ||
| this.redisListeners.set("sub", (msg, channel) => { | ||
| this.onrequest(channel, msg); | ||
| }, true); | ||
| }); | ||
| this.subClient.pSubscribe(this.channel + "*", this.redisListeners.get("psub"), true); | ||
| this.subClient.subscribe([ | ||
| this.requestChannel, | ||
| this.responseChannel, | ||
| this.specificResponseChannel, | ||
| ], this.redisListeners.get("sub"), true); | ||
| } | ||
| else { | ||
| this.redisListeners.set("pmessageBuffer", this.onmessage.bind(this)); | ||
| this.redisListeners.set("messageBuffer", this.onrequest.bind(this)); | ||
| this.subClient.psubscribe(this.channel + "*"); | ||
| this.subClient.on("pmessageBuffer", this.onmessage.bind(this)); | ||
| this.subClient.on("pmessageBuffer", this.redisListeners.get("pmessageBuffer")); | ||
| this.subClient.subscribe([ | ||
| this.requestChannel, | ||
| this.responseChannel, | ||
| specificResponseChannel, | ||
| this.specificResponseChannel, | ||
| ]); | ||
| this.subClient.on("messageBuffer", this.onrequest.bind(this)); | ||
| this.subClient.on("messageBuffer", this.redisListeners.get("messageBuffer")); | ||
| } | ||
@@ -706,3 +715,24 @@ const registerFriendlyErrorHandler = (redisClient) => { | ||
| } | ||
| close() { | ||
| const isRedisV4 = typeof this.pubClient.pSubscribe === "function"; | ||
| if (isRedisV4) { | ||
| this.subClient.pUnsubscribe(this.channel + "*", this.redisListeners.get("psub"), true); | ||
| // There is a bug in redis v4 when unsubscribing multiple channels at once, so we'll unsub one at a time. | ||
| // See https://github.com/redis/node-redis/issues/2052 | ||
| this.subClient.unsubscribe(this.requestChannel, this.redisListeners.get("sub"), true); | ||
| this.subClient.unsubscribe(this.responseChannel, this.redisListeners.get("sub"), true); | ||
| this.subClient.unsubscribe(this.specificResponseChannel, this.redisListeners.get("sub"), true); | ||
| } | ||
| else { | ||
| this.subClient.punsubscribe(this.channel + "*"); | ||
| this.subClient.off("pmessageBuffer", this.redisListeners.get("pmessageBuffer")); | ||
| this.subClient.unsubscribe([ | ||
| this.requestChannel, | ||
| this.responseChannel, | ||
| this.specificResponseChannel, | ||
| ]); | ||
| this.subClient.off("messageBuffer", this.redisListeners.get("messageBuffer")); | ||
| } | ||
| } | ||
| } | ||
| exports.RedisAdapter = RedisAdapter; |
+4
-2
| { | ||
| "name": "@socket.io/redis-adapter", | ||
| "version": "8.0.1", | ||
| "version": "8.1.0", | ||
| "description": "The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers", | ||
@@ -28,5 +28,7 @@ "license": "MIT", | ||
| "notepack.io": "~3.0.1", | ||
| "socket.io-adapter": "~2.4.0", | ||
| "uid2": "1.0.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "socket.io-adapter": "^2.4.0" | ||
| }, | ||
| "devDependencies": { | ||
@@ -33,0 +35,0 @@ "@types/expect.js": "^0.3.29", |
47732
3.85%875
3.92%+ Added
+ Added
+ Added
- Removed
- Removed