mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
refactor!: make RedisBroker require consumer name (#11001)
* refactor(RedisBroker): require consumer name * chore: spelling Co-authored-by: Noel <buechler.noel@outlook.com> --------- Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
committed by
GitHub
parent
6431cea24b
commit
2c750a4e00
@@ -43,7 +43,8 @@ These examples use [ES modules](https://nodejs.org/api/esm.html#enabling).
|
||||
import { PubSubRedisBroker } from '@discordjs/brokers';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
const broker = new PubSubRedisBroker(new Redis());
|
||||
// Considering this only pushes events, the group and name are not important.
|
||||
const broker = new PubSubRedisBroker(new Redis(), { group: 'noop', name: 'noop' });
|
||||
|
||||
await broker.publish('test', 'Hello World!');
|
||||
await broker.destroy();
|
||||
@@ -52,13 +53,22 @@ await broker.destroy();
|
||||
import { PubSubRedisBroker } from '@discordjs/brokers';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
const broker = new PubSubRedisBroker(new Redis());
|
||||
const broker = new PubSubRedisBroker(new Redis(), {
|
||||
// This is the consumer group name. You should make sure to not re-use this
|
||||
// across different applications in your stack, unless you absolutely know
|
||||
// what you're doing.
|
||||
group: 'subscribers',
|
||||
// With the assumption that this service will scale to more than one instance,
|
||||
// you MUST ensure `UNIQUE_CONSUMER_ID` is unique across all of them and
|
||||
// also deterministic (i.e. if instance-1 restarts, it should still be instance-1)
|
||||
name: `consumer-${UNIQUE_CONSUMER_ID}`,
|
||||
});
|
||||
broker.on('test', ({ data, ack }) => {
|
||||
console.log(data);
|
||||
void ack();
|
||||
});
|
||||
|
||||
await broker.subscribe('subscribers', ['test']);
|
||||
await broker.subscribe(['test']);
|
||||
```
|
||||
|
||||
### RPC
|
||||
@@ -68,7 +78,7 @@ await broker.subscribe('subscribers', ['test']);
|
||||
import { RPCRedisBroker } from '@discordjs/brokers';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
const broker = new RPCRedisBroker(new Redis());
|
||||
const broker = new RPCRedisBroker(new Redis(), { group: 'noop', name: 'noop' });
|
||||
|
||||
console.log(await broker.call('testcall', 'Hello World!'));
|
||||
await broker.destroy();
|
||||
@@ -77,14 +87,18 @@ await broker.destroy();
|
||||
import { RPCRedisBroker } from '@discordjs/brokers';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
const broker = new RPCRedisBroker(new Redis());
|
||||
const broker = new RPCRedisBroker(new Redis(), {
|
||||
// Equivalent to the group/name in pubsub, refer to the previous example.
|
||||
group: 'responders',
|
||||
name: `consumer-${UNIQUE_ID}`,
|
||||
});
|
||||
broker.on('testcall', ({ data, ack, reply }) => {
|
||||
console.log('responder', data);
|
||||
void ack();
|
||||
void reply(`Echo: ${data}`);
|
||||
});
|
||||
|
||||
await broker.subscribe('responders', ['testcall']);
|
||||
await broker.subscribe(['testcall']);
|
||||
```
|
||||
|
||||
## Links
|
||||
|
||||
Reference in New Issue
Block a user