Skip to content

Commit

Permalink
feat: add adapter based on sharded Pub/Sub
Browse files Browse the repository at this point in the history
Reference: https://redis.io/docs/manual/pubsub/#sharded-pubsub

Usage:

```js
import { Server } from 'socket.io';
import { createClient } from 'redis';
import { createShardedAdapter } from '@socket.io/redis-adapter';

const pubClient = createClient({ host: 'localhost', port: 6379 });
const subClient = pubClient.duplicate();

await Promise.all([
  pubClient.connect(),
  subClient.connect()
]);

const io = new Server({
  adapter: createShardedAdapter(pubClient, subClient)
});

io.listen(3000);
```

Related: #491
  • Loading branch information
darrachequesne committed Apr 13, 2023
1 parent 1c99cab commit e70b1bd
Show file tree
Hide file tree
Showing 12 changed files with 1,217 additions and 155 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -16,12 +16,12 @@ jobs:
strategy:
matrix:
node-version:
- 12
- 14
- 18

services:
redis:
image: redis
image: redis:7
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
Expand Down
35 changes: 35 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,7 @@
- [CommonJS](#commonjs)
- [ES6 module](#es6-modules)
- [TypeScript](#typescript)
- [Sharded Redis Pub/Sub](#sharded-redis-pubsub)
- [Compatibility table](#compatibility-table)
- [How does it work under the hood?](#how-does-it-work-under-the-hood)
- [API](#api)
Expand Down Expand Up @@ -118,6 +119,40 @@ will properly be broadcast to the clients through the Redis [Pub/Sub mechanism](
If you need to emit events to socket.io instances from a non-socket.io
process, you should use [socket.io-emitter](https://github.com/socketio/socket.io-emitter).

### Sharded Redis Pub/Sub

Sharded Pub/Sub was introduced in Redis 7.0 in order to help scaling the usage of Pub/Sub in cluster mode.

Reference: https://redis.io/docs/manual/pubsub/#sharded-pubsub

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

```js
import { Server } from 'socket.io';
import { createClient } from 'redis';
import { createShardedAdapter } from '@socket.io/redis-adapter';

const pubClient = createClient({ host: 'localhost', port: 6379 });
const subClient = pubClient.duplicate();

await Promise.all([
pubClient.connect(),
subClient.connect()
]);

const io = new Server({
adapter: createShardedAdapter(pubClient, subClient)
});

io.listen(3000);
```

Minimum requirements:

- Redis 7.0
- [`redis@4.6.0`](https://github.com/redis/node-redis/commit/3b1bad229674b421b2bc6424155b20d4d3e45bd1)


## Compatibility table

| Redis Adapter version | Socket.IO server version |
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
@@ -1,6 +1,5 @@
version: '2.0'
services:
redis:
image: redis:5
image: redis:7
ports:
- "6379:6379"

0 comments on commit e70b1bd

Please sign in to comment.