Usage of RedisPubSub to Events
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 = ;const Publisher = Redis; //View https://www.npmjs.com/package/redis#rediscreateclientconst Subscriber = Redis; //View https://www.npmjs.com/package/redis#rediscreateclient //To listen a eventSubscriber;Subscriber; //To publishPublisher; //To stop receive messagesSubscriber;
Using this library
Custom events, multiple callbacks for the same event, supports flat messages and objects.
You write less code.
const RedisPubSub = ;const redisPubSub = ...optionsOfRedis; //View https://www.npmjs.com/package/redis#rediscreateclient //Add bind action to eventredisPubSub; //To publishredisPubSub; //To stop receive messagesredisPubSub;