-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add adapter based on sharded Pub/Sub
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
1 parent
1c99cab
commit e70b1bd
Showing
12 changed files
with
1,217 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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 | ||
- [`[email protected]`](https://github.com/redis/node-redis/commit/3b1bad229674b421b2bc6424155b20d4d3e45bd1) | ||
|
||
|
||
## Compatibility table | ||
|
||
| Redis Adapter version | Socket.IO server version | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
version: '2.0' | ||
services: | ||
redis: | ||
image: redis:5 | ||
image: redis:7 | ||
ports: | ||
- "6379:6379" |
Oops, something went wrong.