PubSubRedisBroker
declare class PubSubRedisBroker<TEvents extends Record<string, any>> extends BaseRedisBroker<TEvents> implements IPubSubBroker<TEvents>
declare class PubSubRedisBroker<TEvents extends Record<string, any>> extends BaseRedisBroker<TEvents> implements IPubSubBroker<TEvents>
PubSub broker powered by Redis
Example
// publisher.js
import { PubSubRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';
const broker = new PubSubRedisBroker({ redisClient: new Redis() });
await broker.publish('test', 'Hello World!');
await broker.destroy();
// subscriber.js
import { PubSubRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';
const broker = new PubSubRedisBroker({ redisClient: new Redis() });
broker.on('test', ({ data, ack }) => {
console.log(data);
void ack();
});
await broker.subscribe('subscribers', ['test']);
// publisher.js
import { PubSubRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';
const broker = new PubSubRedisBroker({ redisClient: new Redis() });
await broker.publish('test', 'Hello World!');
await broker.destroy();
// subscriber.js
import { PubSubRedisBroker } from '@discordjs/brokers';
import Redis from 'ioredis';
const broker = new PubSubRedisBroker({ redisClient: new Redis() });
broker.on('test', ({ data, ack }) => {
console.log(data);
void ack();
});
await broker.subscribe('subscribers', ['test']);
Extends
BaseRedisBroker<TEvents>Implements
IPubSubBroker<TEvents>Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
TEvents | Record<string, any> | No | None |
listening
:
boolean
Whether this broker is currently polling events
Inherited from BaseRedisBrokerReadonly
Options this broker is using
Inherited from BaseRedisBrokerReadonly
STREAM_DATA_KEY
:
Used for Redis queues, see the 3rd argument taken by xadd
Inherited from BaseRedisBrokerReadonly
streamReadClient
:
Redis
Internal copy of the Redis client being used to read incoming payloads
Inherited from BaseRedisBrokerReadonly
subscribedEvents
:
Set<string>
Events this broker has subscribed to
Inherited from BaseRedisBrokerdestroy()
:
Promise<void>
Destroys the broker, closing all connections
Inherited from BaseRedisBrokerProtected
emitEvent(id, group, event, data)
:
void
Name | Type | Optional | Description |
---|---|---|---|
id | Buffer | No | None |
group | string | No | None |
event | string | No | None |
data | unknown | No | None |
Protected
listen(group)
:
Promise<void>
Begins polling for events, firing them to listen
Name | Type | Optional | Description |
---|---|---|---|
group | string | No | None |
publish(event, data)
:
Promise<void>
Publishes an event
Name | Type | Optional | Description |
---|---|---|---|
event | T | No | None |
data | TEvents[T] | No | None |
subscribe(group, events)
:
Promise<void>
Subscribes to the given events, grouping them by the given group name
Name | Type | Optional | Description |
---|---|---|---|
group | string | No | None |
events | (keyof TEvents)[] | No | None |
unsubscribe(group, events)
:
Promise<void>
Unsubscribes from the given events - it's required to pass the same group name as when subscribing for proper cleanup
Name | Type | Optional | Description |
---|---|---|---|
group | string | No | None |
events | (keyof TEvents)[] | No | None |