@ssense/redis-pubsub
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

🡐 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

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

Details

constructor(connection: RedisPubSubConnectionOptions)

Creates a new instance of RedisPubSub

Parameters

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).

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

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

Parameters

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)

Return value

Type Description
RedisPubSub The current RedisPubSub instance

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

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

Parameters

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

Return value

Type Description
RedisPubSub The current RedisPubSub instance

emit(topic: string, message: any)

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

Parameters

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)

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

Readme

Keywords

Package Sidebar

Install

npm i @ssense/redis-pubsub

Weekly Downloads

35

Version

2.0.1

License

ISC

Unpacked Size

18.1 kB

Total Files

8

Last publish

Collaborators

  • jude.seide
  • cmoroney
  • alessandro.commodari
  • jeegna.patel
  • jamesslomka
  • ssenses-agupta
  • expense_ssense
  • toya.okeke
  • quinnlangille
  • ssense-admin
  • kaiz
  • maxime.servonnet.ssense
  • ephremsaour
  • non-senses
  • sam-nicolai.johnston
  • carlossense
  • npm1
  • vijayrajasekar
  • dimitrios.nicolacopoulos
  • sikkavarun
  • lclemence
  • nodir.fayzullaev
  • alexnorms
  • mahdieft