2.0.1 • Published 1 year ago

@ssense/redis-pubsub v2.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

🡐 Go to main README page

Redis Pub/Sub

class RedisPubSub

RedisPubSub allows to quickly set up a client which supports the Redis pub/sub protocol. (see examples here)

Methods

MethodReturnsDescription
constructor(connection: RedisPubSubConnectionOptions)RedisPubSubCreates a new instance of RedisPubSub
on(event: string, listener: (message: PubSubMessage) => void)RedisPubSubRegisters to a Redis topic, adding a listener called when a pub/sub event is received
on(event: 'error', listener: (err: Error) => void)RedisPubSubAdds 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

Details

constructor(connection: RedisPubSubConnectionOptions)

Creates a new instance of RedisPubSub

Parameters

NameTypeRequiredDescription
connectionRedisPubSubConnectionOptionsYesThe parameters used to connect to the Redis pub/sub server

RedisPubSubConnectionOptions properties

NameTypeRequiredDescription
hoststringYesRedis server hostname or IP address
portnumberNoRedis server port (default: 6379)
passwordstringNoPassword for Redis server if any (default: undefined)
tlsEnabledbooleanNoEnable TLS connection (default: false)
compressionEnabledbooleanNoEnable compression (default: false)
passwordstringNoPassword for Redis server if any (default: undefined)
mode'read'|'write'NoUsed 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).

on(event: string, listener: (message: PubSubMessage) => void)

Registers to a Redis topic, adding a listener called when a pub/sub event is received

Parameters

NameTypeRequiredDescription
eventstringYesRedis topic name, you can listen to multiple topics at once using a pattern, eg: *:Created
listenerFunctionYesFunction which will be called when a message is received, will be provided a PubSubMessage object

PubSubMessage properties

NameTypeRequiredDescription
topicstringYesRedis topic on which the message has been sent
dataanyYesMessage data (any object)

Return value

TypeDescription
RedisPubSubThe current RedisPubSub instance

on(event: 'error', listener: (err: Error) => void)

Adds a listener called when an error occurs in the underlying RedisPubSub

Parameters

NameTypeRequiredDescription
event'error'YesThe 'error' string
listenerFunctionYesFunction which will be called on RedisPubSub error, will be provided an Error object

Return value

TypeDescription
RedisPubSubThe current RedisPubSub instance

emit(topic: string, message: any)

Sends a message on Redis pub/sub for a given topic

Parameters

NameTypeRequiredDescription
topicstringYesValid topic name (eg: 'Product:Product:Created')
messageanyYesContent of the message to send, can be any object (needs to be JSON.stringifiable)

Examples

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 });