npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ssense/redis-pubsub

v2.0.2

Published

RedisPubSub allows to quickly set up a client which supports the Redis pub/sub protocol

Downloads

452

Readme

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