redis-pubsub-manager

1.0.1 • Public • Published

Usage of RedisPubSub to Events

NPM version Downloads Build Status Coverage Status

This is a small library, convert callback Subscriptions to events

Installation

npm i redis-pub-sub-manager

or

npm i -D redis-pub-sub-manager

Example

Using native implementation of redis library

Traditionally when subscribing an event when a message arrives we have 2 variables, the channel and the message. So we must always ask for the channel before executing a specific action for that channel.

So we must add a couple of additional lines in our code and we can only send flat messages; so we must encode and decode when sending and receiving a message.

We must also create a connection to publish and another to subscribe.

const Redis = require('redis');
const Publisher  = Redis.createClient(...optionsOfRedis); //View  https://www.npmjs.com/package/redis#rediscreateclient
const Subscriber = Redis.createClient(...optionsOfRedis); //View https://www.npmjs.com/package/redis#rediscreateclient
 
//To listen a event
Subscriber.subscribe("AEventName");
Subscriber.on('message',(Chanel, PlainMessage) => {
    if(Chanel==="AEventName"){
        //Any action
        console.log(JSON.parse(PlainMessage));
    }
});
 
//To publish
Publisher.publish('AEventName',JSON.stringify({Other:15, Msg:'Hello World!'}));
 
//To stop receive messages
Subscriber.unsubscribe("AEventName");

Using this library

Custom events, multiple callbacks for the same event, supports flat messages and objects.

You write less code.

const RedisPubSub = require('redis-pubsub-manager');
const redisPubSub = new RedisPubSub(...optionsOfRedis); //View https://www.npmjs.com/package/redis#rediscreateclient
 
//Add bind action to event
redisPubSub.on("AEventName",(Message)=>{
    console.log(Message); //Object or String
});
 
//To publish
redisPubSub.publish("AEventName",{Other:15, Msg:'Hello World!'});
 
//To stop receive messages
redisPubSub.unbind("AEventName");

Package Sidebar

Install

npm i redis-pubsub-manager

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

10.6 kB

Total Files

13

Last publish

Collaborators

  • wilmerhmg