Skip to content

Commit 82f86ab

Browse files
clean up
1 parent 00a4713 commit 82f86ab

4 files changed

Lines changed: 29 additions & 51 deletions

File tree

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ The `@socket.io/redis-adapter` package allows broadcasting packets between multi
1818
- [With the `ioredis` package](#with-the-ioredis-package)
1919
- [With the `ioredis` package and a Redis cluster](#with-the-ioredis-package-and-a-redis-cluster)
2020
- [With Redis sharded Pub/Sub](#with-redis-sharded-pubsub)
21-
- [With the `ioredis` package and a Redis cluster](#with-the-ioredis-package-and-a-redis-cluster-1)
21+
- [With `redis`](#with-redis)
22+
- [With `ioredis`](#with-ioredis)
2223
- [Options](#options)
2324
- [Default adapter](#default-adapter)
2425
- [Sharded adapter](#sharded-adapter)
@@ -162,6 +163,13 @@ Reference: https://redis.io/docs/interact/pubsub/#sharded-pubsub
162163

163164
A dedicated adapter can be created with the `createShardedAdapter()` method:
164165

166+
#### With `redis`
167+
168+
Minimum requirements:
169+
170+
- Redis 7.0
171+
- [`redis@4.6.0`](https://github.com/redis/node-redis/commit/3b1bad229674b421b2bc6424155b20d4d3e45bd1)
172+
165173
```js
166174
import { Server } from "socket.io";
167175
import { createClient } from "redis";
@@ -182,14 +190,14 @@ const io = new Server({
182190
io.listen(3000);
183191
```
184192

193+
#### With `ioredis`
194+
185195
Minimum requirements:
186196

187197
- Redis 7.0
188-
- [`redis@4.6.0`](https://github.com/redis/node-redis/commit/3b1bad229674b421b2bc6424155b20d4d3e45bd1)
198+
- [`ioredis@5.9.0`](https://github.com/redis/ioredis/pull/1956)
189199

190-
#### With the `ioredis` package and a Redis cluster
191-
192-
Starting with `ioredis@5.9.0`, you can use the sharded adapter with an ioredis Cluster by enabling the `shardedSubscribers` option:
200+
Please note that the `shardedSubscribers` option is required to enable sharded Pub/Sub.
193201

194202
```js
195203
import { Cluster } from "ioredis";
@@ -224,8 +232,6 @@ const io = new Server({
224232
io.listen(3000);
225233
```
226234

227-
Reference: https://github.com/redis/ioredis/pull/1956
228-
229235
## Options
230236

231237
### Default adapter

lib/sharded-adapter.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
} from "socket.io-adapter";
88
import { decode, encode } from "notepack.io";
99
import {
10+
checkConfiguration,
1011
hasBinary,
11-
hasShardedSubscribers,
12-
isIoRedisCluster,
1312
PUBSUB,
1413
SPUBLISH,
1514
SSUBSCRIBE,
@@ -83,6 +82,9 @@ class ShardedRedisAdapter extends ClusterAdapter {
8382

8483
constructor(nsp, pubClient, subClient, opts: ShardedRedisAdapterOptions) {
8584
super(nsp);
85+
86+
checkConfiguration(subClient);
87+
8688
this.pubClient = pubClient;
8789
this.subClient = subClient;
8890
this.opts = Object.assign(
@@ -93,15 +95,6 @@ class ShardedRedisAdapter extends ClusterAdapter {
9395
opts
9496
);
9597

96-
// Validate ioredis Cluster configuration
97-
if (isIoRedisCluster(subClient) && !hasShardedSubscribers(subClient)) {
98-
throw new Error(
99-
"When using the sharded adapter with an ioredis Cluster, " +
100-
"you must enable the 'shardedSubscribers' option. " +
101-
"See https://github.com/redis/ioredis/pull/1956"
102-
);
103-
}
104-
10598
this.channel = `${this.opts.channelPrefix}#${nsp.name}#`;
10699
this.responseChannel = `${this.opts.channelPrefix}#${nsp.name}#${this.uid}#`;
107100

lib/util.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,19 @@ function isRedisV4Client(redisClient: any) {
5353
}
5454

5555
/**
56-
* Whether the client is an ioredis Cluster instance
57-
*
56+
* Check the configuration of the client to ensure it is compatible with the sharded adapter
5857
* @param redisClient
5958
*/
60-
export function isIoRedisCluster(redisClient: any) {
61-
return redisClient.constructor.name === "Cluster" || redisClient.isCluster;
62-
}
63-
64-
/**
65-
* Whether the ioredis Cluster has shardedSubscribers enabled
66-
*
67-
* @param redisClient
68-
*
69-
* @see https://github.com/redis/ioredis/pull/1956
70-
*/
71-
export function hasShardedSubscribers(redisClient: any) {
72-
return (
73-
isIoRedisCluster(redisClient) &&
74-
redisClient.options?.shardedSubscribers === true
75-
);
59+
export function checkConfiguration(redisClient: any) {
60+
const isIORedisCluster =
61+
typeof redisClient.ssubscribe === "function" && redisClient.isCluster;
62+
if (isIORedisCluster && redisClient.options?.shardedSubscribers !== true) {
63+
throw new Error(
64+
"When using the sharded adapter with an ioredis Cluster, " +
65+
"you must enable the 'shardedSubscribers' option. " +
66+
"See https://github.com/redis/ioredis/pull/1956"
67+
);
68+
}
7669
}
7770

7871
const kHandlers = Symbol("handlers");

test/test-runner.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ const clusterNodes = [
3838
},
3939
];
4040

41-
// NAT mapping for ioredis Cluster (Docker container returns internal IP)
42-
const ioredisNatMap = {
43-
"172.20.0.3:7000": { host: "localhost", port: 7000 },
44-
"172.20.0.3:7001": { host: "localhost", port: 7001 },
45-
"172.20.0.3:7002": { host: "localhost", port: 7002 },
46-
"172.20.0.3:7003": { host: "localhost", port: 7003 },
47-
"172.20.0.3:7004": { host: "localhost", port: 7004 },
48-
"172.20.0.3:7005": { host: "localhost", port: 7005 },
49-
};
50-
5141
function testSuite(
5242
createAdapter: any,
5343
redisPackage: string = "redis@4",
@@ -149,9 +139,7 @@ describe("@socket.io/redis-adapter", () => {
149139

150140
describe("ioredis cluster", () =>
151141
testSuite(async () => {
152-
const pubClient = new Cluster(clusterNodes, {
153-
natMap: ioredisNatMap,
154-
});
142+
const pubClient = new Cluster(clusterNodes);
155143
const subClient = pubClient.duplicate();
156144

157145
return [
@@ -271,13 +259,11 @@ describe("@socket.io/redis-adapter", () => {
271259
true
272260
));
273261

274-
// Fixed in ioredis 5.9.0, see https://github.com/redis/ioredis/pull/1956
275262
describe("[sharded] ioredis cluster", () =>
276263
testSuite(
277264
async () => {
278265
const pubClient = new Cluster(clusterNodes, {
279266
shardedSubscribers: true,
280-
natMap: ioredisNatMap,
281267
});
282268
const subClient = pubClient.duplicate();
283269

0 commit comments

Comments
 (0)