Skip to content

Commit c2d668b

Browse files
seta-chuang-0909darrachequesne
authored andcommitted
test: add test with ioredis and Redis cluster (#570)
Support for sharded pub/sub was added in `ioredis@5.9.0`. Related: - redis/ioredis#1759 - redis/ioredis#1956
1 parent cdb5535 commit c2d668b

File tree

4 files changed

+80
-25
lines changed

4 files changed

+80
-25
lines changed

README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +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 `redis`](#with-redis)
22+
- [With `ioredis`](#with-ioredis)
2123
- [Options](#options)
2224
- [Default adapter](#default-adapter)
2325
- [Sharded adapter](#sharded-adapter)
@@ -161,6 +163,13 @@ Reference: https://redis.io/docs/interact/pubsub/#sharded-pubsub
161163

162164
A dedicated adapter can be created with the `createShardedAdapter()` method:
163165

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+
164173
```js
165174
import { Server } from "socket.io";
166175
import { createClient } from "redis";
@@ -181,12 +190,47 @@ const io = new Server({
181190
io.listen(3000);
182191
```
183192

193+
#### With `ioredis`
194+
184195
Minimum requirements:
185196

186197
- Redis 7.0
187-
- [`redis@4.6.0`](https://github.com/redis/node-redis/commit/3b1bad229674b421b2bc6424155b20d4d3e45bd1)
198+
- [`ioredis@5.9.0`](https://github.com/redis/ioredis/pull/1956)
199+
200+
Please note that the `shardedSubscribers` option is required to enable sharded Pub/Sub.
201+
202+
```js
203+
import { Cluster } from "ioredis";
204+
import { Server } from "socket.io";
205+
import { createShardedAdapter } from "@socket.io/redis-adapter";
206+
207+
const pubClient = new Cluster(
208+
[
209+
{
210+
host: "localhost",
211+
port: 7000,
212+
},
213+
{
214+
host: "localhost",
215+
port: 7001,
216+
},
217+
{
218+
host: "localhost",
219+
port: 7002,
220+
},
221+
],
222+
{
223+
shardedSubscribers: true,
224+
}
225+
);
226+
const subClient = pubClient.duplicate();
188227

189-
Note: it is not currently possible to use the sharded adapter with the `ioredis` package and a Redis cluster ([reference](https://github.com/luin/ioredis/issues/1759)).
228+
const io = new Server({
229+
adapter: createShardedAdapter(pubClient, subClient)
230+
});
231+
232+
io.listen(3000);
233+
```
190234

191235
## Options
192236

package-lock.json

Lines changed: 29 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@types/mocha": "^8.2.1",
3333
"@types/node": "^14.14.7",
3434
"expect.js": "0.3.1",
35-
"ioredis": "^5.3.2",
35+
"ioredis": "^5.9.1",
3636
"mocha": "^10.1.0",
3737
"nyc": "^15.1.0",
3838
"prettier": "^2.8.7",

test/test-runner.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,12 @@ describe("@socket.io/redis-adapter", () => {
259259
true
260260
));
261261

262-
// FIXME see https://github.com/luin/ioredis/issues/1759
263-
describe.skip("[sharded] ioredis cluster", () =>
262+
describe("[sharded] ioredis cluster", () =>
264263
testSuite(
265264
async () => {
266-
const pubClient = new Cluster(clusterNodes);
265+
const pubClient = new Cluster(clusterNodes, {
266+
shardedSubscribers: true,
267+
});
267268
const subClient = pubClient.duplicate();
268269

269270
return [

0 commit comments

Comments
 (0)