Skip to content

Commit

Permalink
refactor: reuse same PUBSUB method
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed May 14, 2023
1 parent 2da8d9e commit 4c372a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 45 deletions.
46 changes: 2 additions & 44 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uid2 = require("uid2");
import msgpack = require("notepack.io");
import { Adapter, BroadcastOptions, Room } from "socket.io-adapter";
import { parseNumSubResponse, sumValues } from "./util";
import { PUBSUB } from "./util";

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

Expand Down Expand Up @@ -891,49 +891,7 @@ export class RedisAdapter extends Adapter {
}

override serverCount(): Promise<number> {
if (
this.pubClient.constructor.name === "Cluster" ||
this.pubClient.isCluster
) {
// ioredis cluster
const nodes = this.pubClient.nodes();
return Promise.all(
nodes.map((node) =>
node
.send_command("pubsub", ["numsub", this.requestChannel])
.then(parseNumSubResponse)
)
).then(sumValues);
} else if (typeof this.pubClient.pSubscribe === "function") {
// node-redis client
const isCluster = Array.isArray(this.pubClient.masters);
if (isCluster) {
const nodes = this.pubClient.masters;
return Promise.all(
nodes.map((node) => {
return node.client
.sendCommand(["pubsub", "numsub", this.requestChannel])
.then(parseNumSubResponse);
})
).then(sumValues);
} else {
return this.pubClient
.sendCommand(["pubsub", "numsub", this.requestChannel])
.then(parseNumSubResponse);
}
} else {
// ioredis or node-redis v3 client
return new Promise((resolve, reject) => {
this.pubClient.send_command(
"pubsub",
["numsub", this.requestChannel],
(err, numSub) => {
if (err) return reject(err);
resolve(parseNumSubResponse(numSub));
}
);
});
}
return PUBSUB(this.pubClient, "NUMSUB", this.requestChannel);
}

close(): Promise<void> | void {
Expand Down
6 changes: 5 additions & 1 deletion lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,18 @@ export function SPUBLISH(

export function PUBSUB(redisClient: any, arg: string, channel: string) {
if (redisClient.constructor.name === "Cluster" || redisClient.isCluster) {
// ioredis cluster
return Promise.all(
redisClient.nodes().map((node) => {
return node
.sendCommand(["PUBSUB", arg, channel])
.send_command("PUBSUB", [arg, channel])
.then(parseNumSubResponse);
})
).then(sumValues);
} else if (isRedisV4Client(redisClient)) {
const isCluster = Array.isArray(redisClient.masters);
if (isCluster) {
// redis@4 cluster
const nodes = redisClient.masters;
return Promise.all(
nodes.map((node) => {
Expand All @@ -117,11 +119,13 @@ export function PUBSUB(redisClient: any, arg: string, channel: string) {
})
).then(sumValues);
} else {
// redis@4 standalone
return redisClient
.sendCommand(["PUBSUB", arg, channel])
.then(parseNumSubResponse);
}
} else {
// ioredis / redis@3 standalone
return new Promise((resolve, reject) => {
redisClient.send_command("PUBSUB", [arg, channel], (err, numSub) => {
if (err) return reject(err);
Expand Down

0 comments on commit 4c372a8

Please sign in to comment.