-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for sharded PubSub (#2373)
* refactor pubsub, add support for sharded pub sub * run tests in redis 7 only, fix PUBSUB SHARDCHANNELS test * add some comments and fix some bugs * PubSubType, not PubSubTypes 🤦♂️ * remove test.txt * fix some bugs, add tests * add some tests * fix #2345 - allow PING in PubSub mode (remove client side validation) * remove .only * revert changes in cluster/index.ts * fix tests minimum version * handle server sunsubscribe * add 'sharded-channel-moved' event to docs, improve the events section in the main README (fix #2302) * exit "resubscribe" if pubsub not active * Update commands-queue.ts * Release [email protected] * WIP * use `node:util` instead of `node:util/types` (to support node 14) * run PubSub resharding test with Redis 7+ * fix inconsistency in live resharding test * add some tests * fix iterateAllNodes when starting from a replica * fix iterateAllNodes random * fix slotNodesIterator * fix slotNodesIterator * clear pubSubNode when node in use * wait for all nodes cluster state to be ok before testing * `cluster.minimizeConections` tests * `client.reconnectStrategry = false | 0` tests * sharded pubsub + cluster 🎉 * add minimum version to sharded pubsub tests * add cluster sharded pubsub live reshard test, use stable dockers for tests, make sure to close pubsub clients when a node disconnects from the cluster * fix "ssubscribe & sunsubscribe" test * lock search docker to 2.4.9 * change numberOfMasters default to 2 * use edge for bloom * add tests * add back getMasters and getSlotMaster as deprecated functions * add some tests * fix reconnect strategy + docs * sharded pubsub docs * Update pub-sub.md * some jsdoc, docs, cluster topology test * clean pub-sub docs Co-authored-by: Simon Prickett <[email protected]> * reconnect startegy docs and bug fix Co-authored-by: Simon Prickett <[email protected]> * refine jsdoc and some docs Co-authored-by: Simon Prickett <[email protected]> * I'm stupid * fix cluster topology test * fix cluster topology test * Update README.md * Update clustering.md * Update pub-sub.md Co-authored-by: Simon Prickett <[email protected]>
- Loading branch information
Showing
41 changed files
with
2,357 additions
and
710 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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Pub/Sub | ||
|
||
The Pub/Sub API is implemented by `RedisClient` and `RedisCluster`. | ||
|
||
## Pub/Sub with `RedisClient` | ||
|
||
Pub/Sub requires a dedicated stand-alone client. You can easily get one by `.duplicate()`ing an existing `RedisClient`: | ||
|
||
```typescript | ||
const subscriber = client.duplicate(); | ||
subscribe.on('error', err => console.error(err)); | ||
await subscriber.connect(); | ||
``` | ||
|
||
When working with a `RedisCluster`, this is handled automatically for you. | ||
|
||
### `sharded-channel-moved` event | ||
|
||
`RedisClient` emits the `sharded-channel-moved` event when the ["cluster slot"](https://redis.io/docs/reference/cluster-spec/#key-distribution-model) of a subscribed [Sharded Pub/Sub](https://redis.io/docs/manual/pubsub/#sharded-pubsub) channel has been moved to another shard. | ||
|
||
The event listener signature is as follows: | ||
```typescript | ||
( | ||
channel: string, | ||
listeners: { | ||
buffers: Set<Listener>; | ||
strings: Set<Listener>; | ||
} | ||
)`. | ||
``` | ||
|
||
## Subscribing | ||
|
||
```javascript | ||
const listener = (message, channel) => console.log(message, channel); | ||
await client.subscribe('channel', listener); | ||
await client.pSubscribe('channe*', listener); | ||
// Use sSubscribe for sharded Pub/Sub: | ||
await client.sSubscribe('channel', listener); | ||
``` | ||
|
||
## Publishing | ||
|
||
```javascript | ||
await client.publish('channel', 'message'); | ||
// Use sPublish for sharded Pub/Sub: | ||
await client.sPublish('channel', 'message'); | ||
``` | ||
|
||
## Unsubscribing | ||
|
||
The code below unsubscribes all listeners from all channels. | ||
|
||
```javascript | ||
await client.unsubscribe(); | ||
await client.pUnsubscribe(); | ||
// Use sUnsubscribe for sharded Pub/Sub: | ||
await client.sUnsubscribe(); | ||
``` | ||
|
||
To unsubscribe from specific channels: | ||
|
||
```javascript | ||
await client.unsubscribe('channel'); | ||
await client.unsubscribe(['1', '2']); | ||
``` | ||
|
||
To unsubscribe a specific listener: | ||
|
||
```javascript | ||
await client.unsubscribe('channel', listener); | ||
``` | ||
|
||
## Buffers | ||
|
||
Publishing and subscribing using `Buffer`s is also supported: | ||
|
||
```javascript | ||
await subscriber.subscribe('channel', message => { | ||
console.log(message); // <Buffer 6d 65 73 73 61 67 65> | ||
}, true); // true = subscribe in `Buffer` mode. | ||
await subscriber.publish(Buffer.from('channel'), Buffer.from('message')); | ||
``` | ||
|
||
> NOTE: Buffers and strings are supported both for the channel name and the message. You can mix and match these as desired. |
Oops, something went wrong.