class RedisPubSub
RedisPubSub allows to quickly set up a client which supports the Redis pub/sub protocol. (see examples here)
Method | Returns | Description |
---|---|---|
constructor(connection: RedisPubSubConnectionOptions ) |
RedisPubSub |
Creates a new instance of RedisPubSub |
on(event: string , listener: (message: PubSubMessage ) => void) |
RedisPubSub |
Registers to a Redis topic, adding a listener called when a pub/sub event is received |
on(event: 'error' , listener: (err: Error ) => void) |
RedisPubSub |
Adds a listener called when an error occurs in the underlying RedisPubSub |
emit(topic: string , message: any ) |
Promise<void> |
Sends a message on Redis pub/sub for a given topic |
Creates a new instance of RedisPubSub
Name | Type | Required | Description |
---|---|---|---|
connection | RedisPubSubConnectionOptions |
Yes | The parameters used to connect to the Redis pub/sub server |
RedisPubSubConnectionOptions properties
Name | Type | Required | Description |
---|---|---|---|
host | string |
Yes | Redis server hostname or IP address |
port | number |
No | Redis server port (default: 6379 ) |
password | string |
No | Password for Redis server if any (default: undefined ) |
tlsEnabled | boolean |
No | Enable TLS connection (default: false ) |
compressionEnabled | boolean |
No | Enable compression (default: false ) |
password | string |
No | Password for Redis server if any (default: undefined ) |
mode |
'read' |'write'
|
No | Used to save resources, by avoiding extra connections to Redis if your application only needs to read or write to the pub/sub system. Do not set to get a bi-directional connection (read & write, which is the default). Use read to use a read-only connection (if used, the emit method will be deactivated).Use write to use a write only connection (if used, the on method will be deactivated, except for the error event). |
Registers to a Redis topic, adding a listener called when a pub/sub event is received
Name | Type | Required | Description |
---|---|---|---|
event | string |
Yes | Redis topic name, you can listen to multiple topics at once using a pattern, eg: *:Created
|
listener | Function |
Yes | Function which will be called when a message is received, will be provided a PubSubMessage object |
PubSubMessage properties
Name | Type | Required | Description |
---|---|---|---|
topic | string |
Yes | Redis topic on which the message has been sent |
data | any |
Yes | Message data (any object) |
Type | Description |
---|---|
RedisPubSub |
The current RedisPubSub instance |
Adds a listener called when an error occurs in the underlying RedisPubSub
Name | Type | Required | Description |
---|---|---|---|
event | 'error' |
Yes | The 'error' string |
listener | Function |
Yes | Function which will be called on RedisPubSub error, will be provided an Error object |
Type | Description |
---|---|
RedisPubSub |
The current RedisPubSub instance |
Sends a message on Redis pub/sub for a given topic
Name | Type | Required | Description |
---|---|---|---|
topic | string |
Yes | Valid topic name (eg: 'Product:Product:Created' ) |
message | any |
Yes | Content of the message to send, can be any object (needs to be JSON.stringifiable) |
import { RedisPubSub } from '@ssense/redis-pubsub';
// Instantiate the client with the minimum required parameters
const client = new RedisPubSub({ host: 'localhost' });
// Register to a simple topic
client.on('Product:Product:Created', (message) => {
console.log(message);
// Will display: {topic: 'Product:Product:Created', data: {id: 123456789}}
});
// Register using a pattern, to listen to multiple topics at once
client.on('*:Created', (message) => {
console.log(message);
// Will display: {topic: 'Product:Product:Created', data: {id: 123456789}}
});
// Register to a special "error" topic, called if an internal error occurs in the RedisPubSub class
client.on('error', (err: Error) => {
// Manage the error
console.error('An error occurred', err);
});
// Send a message on a specific topic, which will trigger the two listeners above
client.emit('Product:Product:Created', { id: 123456789 });
// Send a message on another topic, which will only trigger the second listener above
client.emit('Product:Brand:Created', { id: 123456789 });