Skip to content

Commit 4c372a8

Browse files
refactor: reuse same PUBSUB method
1 parent 2da8d9e commit 4c372a8

File tree

2 files changed

+7
-45
lines changed

2 files changed

+7
-45
lines changed

lib/index.ts

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uid2 = require("uid2");
22
import msgpack = require("notepack.io");
33
import { Adapter, BroadcastOptions, Room } from "socket.io-adapter";
4-
import { parseNumSubResponse, sumValues } from "./util";
4+
import { PUBSUB } from "./util";
55

66
const debug = require("debug")("socket.io-redis");
77

@@ -891,49 +891,7 @@ export class RedisAdapter extends Adapter {
891891
}
892892

893893
override serverCount(): Promise<number> {
894-
if (
895-
this.pubClient.constructor.name === "Cluster" ||
896-
this.pubClient.isCluster
897-
) {
898-
// ioredis cluster
899-
const nodes = this.pubClient.nodes();
900-
return Promise.all(
901-
nodes.map((node) =>
902-
node
903-
.send_command("pubsub", ["numsub", this.requestChannel])
904-
.then(parseNumSubResponse)
905-
)
906-
).then(sumValues);
907-
} else if (typeof this.pubClient.pSubscribe === "function") {
908-
// node-redis client
909-
const isCluster = Array.isArray(this.pubClient.masters);
910-
if (isCluster) {
911-
const nodes = this.pubClient.masters;
912-
return Promise.all(
913-
nodes.map((node) => {
914-
return node.client
915-
.sendCommand(["pubsub", "numsub", this.requestChannel])
916-
.then(parseNumSubResponse);
917-
})
918-
).then(sumValues);
919-
} else {
920-
return this.pubClient
921-
.sendCommand(["pubsub", "numsub", this.requestChannel])
922-
.then(parseNumSubResponse);
923-
}
924-
} else {
925-
// ioredis or node-redis v3 client
926-
return new Promise((resolve, reject) => {
927-
this.pubClient.send_command(
928-
"pubsub",
929-
["numsub", this.requestChannel],
930-
(err, numSub) => {
931-
if (err) return reject(err);
932-
resolve(parseNumSubResponse(numSub));
933-
}
934-
);
935-
});
936-
}
894+
return PUBSUB(this.pubClient, "NUMSUB", this.requestChannel);
937895
}
938896

939897
close(): Promise<void> | void {

lib/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,18 @@ export function SPUBLISH(
9898

9999
export function PUBSUB(redisClient: any, arg: string, channel: string) {
100100
if (redisClient.constructor.name === "Cluster" || redisClient.isCluster) {
101+
// ioredis cluster
101102
return Promise.all(
102103
redisClient.nodes().map((node) => {
103104
return node
104-
.sendCommand(["PUBSUB", arg, channel])
105+
.send_command("PUBSUB", [arg, channel])
105106
.then(parseNumSubResponse);
106107
})
107108
).then(sumValues);
108109
} else if (isRedisV4Client(redisClient)) {
109110
const isCluster = Array.isArray(redisClient.masters);
110111
if (isCluster) {
112+
// redis@4 cluster
111113
const nodes = redisClient.masters;
112114
return Promise.all(
113115
nodes.map((node) => {
@@ -117,11 +119,13 @@ export function PUBSUB(redisClient: any, arg: string, channel: string) {
117119
})
118120
).then(sumValues);
119121
} else {
122+
// redis@4 standalone
120123
return redisClient
121124
.sendCommand(["PUBSUB", arg, channel])
122125
.then(parseNumSubResponse);
123126
}
124127
} else {
128+
// ioredis / redis@3 standalone
125129
return new Promise((resolve, reject) => {
126130
redisClient.send_command("PUBSUB", [arg, channel], (err, numSub) => {
127131
if (err) return reject(err);

0 commit comments

Comments
 (0)