Coder Social home page Coder Social logo

redis-pub-sub-manager's Introduction

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

redis-pub-sub-manager's People

Contributors

wilmerhmg avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.