@socket.io/redis-adapter
Advanced tools
Comparing version 7.0.1 to 7.1.0
@@ -0,1 +1,12 @@ | ||
# [7.1.0](https://github.com/socketio/socket.io-redis-adapter/compare/7.0.1...7.1.0) (2021-11-29) | ||
### Features | ||
* add support for redis v4 ([aa681b3](https://github.com/socketio/socket.io-redis-adapter/commit/aa681b3bc914358d206ab35761d291a466ac18da)) | ||
* do not emit "error" events anymore ([8e5c84f](https://github.com/socketio/socket.io-redis-adapter/commit/8e5c84f7edcda85a6f7e36c04ebd74152c1cade1)) | ||
* send response to the requesting node only ([f66de11](https://github.com/socketio/socket.io-redis-adapter/commit/f66de114a4581b692da759015def0373c619aab7)) | ||
## [7.0.1](https://github.com/socketio/socket.io-redis-adapter/compare/7.0.0...7.0.1) (2021-11-15) | ||
@@ -2,0 +13,0 @@ |
@@ -13,2 +13,14 @@ import { Adapter, BroadcastOptions, Room, SocketId } from "socket.io-adapter"; | ||
requestsTimeout: number; | ||
/** | ||
* Whether to publish a response to the channel specific to the requesting node. | ||
* | ||
* - if true, the response will be published to `${key}-request#${nsp}#${uid}#` | ||
* - if false, the response will be published to `${key}-request#${nsp}#` | ||
* | ||
* This option currently defaults to false for backward compatibility, but will be set to true in the next major | ||
* release. | ||
* | ||
* @default false | ||
*/ | ||
publishOnSpecificResponseChannel: boolean; | ||
} | ||
@@ -30,2 +42,3 @@ /** | ||
readonly requestsTimeout: number; | ||
readonly publishOnSpecificResponseChannel: boolean; | ||
private readonly channel; | ||
@@ -60,2 +73,9 @@ private readonly requestChannel; | ||
/** | ||
* Send the response to the requesting node | ||
* @param request | ||
* @param response | ||
* @private | ||
*/ | ||
private publishResponse; | ||
/** | ||
* Called on response from another node | ||
@@ -62,0 +82,0 @@ * |
@@ -67,2 +67,3 @@ "use strict"; | ||
this.requestsTimeout = opts.requestsTimeout || 5000; | ||
this.publishOnSpecificResponseChannel = !!opts.publishOnSpecificResponseChannel; | ||
const prefix = opts.key || "socket.io"; | ||
@@ -72,13 +73,31 @@ this.channel = prefix + "#" + nsp.name + "#"; | ||
this.responseChannel = prefix + "-response#" + this.nsp.name + "#"; | ||
const onError = (err) => { | ||
if (err) { | ||
this.emit("error", err); | ||
} | ||
const specificResponseChannel = this.responseChannel + this.uid + "#"; | ||
const isRedisV4 = typeof this.pubClient.pSubscribe === "function"; | ||
if (isRedisV4) { | ||
this.subClient.pSubscribe(this.channel + "*", (msg, channel) => { | ||
this.onmessage(null, channel, msg); | ||
}, true); | ||
this.subClient.subscribe([this.requestChannel, this.responseChannel, specificResponseChannel], (msg, channel) => { | ||
this.onrequest(channel, msg); | ||
}); | ||
} | ||
else { | ||
this.subClient.psubscribe(this.channel + "*"); | ||
this.subClient.on("pmessageBuffer", this.onmessage.bind(this)); | ||
this.subClient.subscribe([ | ||
this.requestChannel, | ||
this.responseChannel, | ||
specificResponseChannel, | ||
]); | ||
this.subClient.on("messageBuffer", this.onrequest.bind(this)); | ||
} | ||
const registerFriendlyErrorHandler = (redisClient) => { | ||
redisClient.on("error", () => { | ||
if (redisClient.listenerCount("error") === 1) { | ||
console.warn("missing 'error' handler on this Redis client"); | ||
} | ||
}); | ||
}; | ||
this.subClient.psubscribe(this.channel + "*", onError); | ||
this.subClient.on("pmessageBuffer", this.onmessage.bind(this)); | ||
this.subClient.subscribe([this.requestChannel, this.responseChannel], onError); | ||
this.subClient.on("messageBuffer", this.onrequest.bind(this)); | ||
this.pubClient.on("error", onError); | ||
this.subClient.on("error", onError); | ||
registerFriendlyErrorHandler(this.pubClient); | ||
registerFriendlyErrorHandler(this.subClient); | ||
} | ||
@@ -137,3 +156,3 @@ /** | ||
catch (err) { | ||
this.emit("error", err); | ||
debug("ignoring malformed request"); | ||
return; | ||
@@ -153,3 +172,3 @@ } | ||
}); | ||
this.pubClient.publish(this.responseChannel, response); | ||
this.publishResponse(request, response); | ||
break; | ||
@@ -164,3 +183,3 @@ case RequestType.ALL_ROOMS: | ||
}); | ||
this.pubClient.publish(this.responseChannel, response); | ||
this.publishResponse(request, response); | ||
break; | ||
@@ -183,3 +202,3 @@ case RequestType.REMOTE_JOIN: | ||
}); | ||
this.pubClient.publish(this.responseChannel, response); | ||
this.publishResponse(request, response); | ||
break; | ||
@@ -202,3 +221,3 @@ case RequestType.REMOTE_LEAVE: | ||
}); | ||
this.pubClient.publish(this.responseChannel, response); | ||
this.publishResponse(request, response); | ||
break; | ||
@@ -221,3 +240,3 @@ case RequestType.REMOTE_DISCONNECT: | ||
}); | ||
this.pubClient.publish(this.responseChannel, response); | ||
this.publishResponse(request, response); | ||
break; | ||
@@ -246,3 +265,3 @@ case RequestType.REMOTE_FETCH: | ||
}); | ||
this.pubClient.publish(this.responseChannel, response); | ||
this.publishResponse(request, response); | ||
break; | ||
@@ -281,2 +300,15 @@ case RequestType.SERVER_SIDE_EMIT: | ||
/** | ||
* Send the response to the requesting node | ||
* @param request | ||
* @param response | ||
* @private | ||
*/ | ||
publishResponse(request, response) { | ||
const responseChannel = this.publishOnSpecificResponseChannel | ||
? `${this.responseChannel}${request.uid}#` | ||
: this.responseChannel; | ||
debug("publishing response to channel %s", responseChannel); | ||
this.pubClient.publish(responseChannel, response); | ||
} | ||
/** | ||
* Called on response from another node | ||
@@ -292,3 +324,3 @@ * | ||
catch (err) { | ||
this.emit("error", err); | ||
debug("ignoring malformed response"); | ||
return; | ||
@@ -403,2 +435,3 @@ } | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
@@ -440,2 +473,3 @@ type: RequestType.SOCKETS, | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
@@ -477,2 +511,3 @@ type: RequestType.ALL_ROOMS, | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
@@ -513,2 +548,3 @@ type: RequestType.REMOTE_JOIN, | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
@@ -549,2 +585,3 @@ type: RequestType.REMOTE_LEAVE, | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
@@ -583,2 +620,3 @@ type: RequestType.REMOTE_DISCONNECT, | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
requestId, | ||
@@ -615,2 +653,3 @@ type: RequestType.REMOTE_FETCH, | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
type: RequestType.REMOTE_JOIN, | ||
@@ -631,2 +670,3 @@ opts: { | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
type: RequestType.REMOTE_LEAVE, | ||
@@ -647,2 +687,3 @@ opts: { | ||
const request = JSON.stringify({ | ||
uid: this.uid, | ||
type: RequestType.REMOTE_DISCONNECT, | ||
@@ -719,2 +760,7 @@ opts: { | ||
} | ||
else if (typeof this.pubClient.pSubscribe === "function") { | ||
return this.pubClient | ||
.sendCommand(["pubsub", "numsub", this.requestChannel]) | ||
.then((res) => parseInt(res[1], 10)); | ||
} | ||
else { | ||
@@ -721,0 +767,0 @@ // RedisClient or Redis |
{ | ||
"name": "@socket.io/redis-adapter", | ||
"version": "7.0.1", | ||
"version": "7.1.0", | ||
"description": "The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers", | ||
@@ -16,3 +16,7 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "npm run format:check && tsc && nyc mocha --require ts-node/register test/index.ts", | ||
"test": "npm run format:check && tsc && npm run test:redis-v4 && npm run test:redis-v4-specific-channel && npm run test:redis-v3 && npm run test:ioredis", | ||
"test:redis-v4": "nyc mocha --bail --require ts-node/register test/index.ts", | ||
"test:redis-v4-specific-channel": "SPECIFIC_CHANNEL=1 nyc mocha --bail --require ts-node/register test/index.ts", | ||
"test:redis-v3": "REDIS_CLIENT=redis-v3 nyc mocha --bail --require ts-node/register test/index.ts", | ||
"test:ioredis": "REDIS_CLIENT=ioredis nyc mocha --bail --require ts-node/register test/index.ts", | ||
"format:check": "prettier --parser typescript --check 'lib/**/*.ts' 'test/**/*.ts'", | ||
@@ -32,3 +36,2 @@ "format:fix": "prettier --parser typescript --write 'lib/**/*.ts' 'test/**/*.ts'", | ||
"@types/node": "^14.14.7", | ||
"@types/redis": "^2.8.28", | ||
"expect.js": "0.3.1", | ||
@@ -39,3 +42,4 @@ "ioredis": "^4.0.0", | ||
"prettier": "^2.1.2", | ||
"redis": "^3.1.2", | ||
"redis": "^4.0.0", | ||
"redis-v3": "npm:redis@^3.1.2", | ||
"socket.io": "^4.1.1", | ||
@@ -42,0 +46,0 @@ "socket.io-client": "^4.1.1", |
51090
897