Coder Social home page Coder Social logo

redis-eventemitter's Introduction

redis-eventemitter build status

Use redis as pubsub using a simple eventemitter.

npm install redis-eventemitter

Usage

var redis = require('redis-eventemitter');

var pubsub = redis({
	prefix: 'production:',
	url: 'redis://myuser:mypass@localhost:6379/'
	// port: 6379,
	// host: '127.0.0.1',
	// auth_pass: 'mypassword'

	// in case you want to control Redis clients
	// creation you can specify pub/sub clients pair:
	// pub: pubClient,
	// sub: subClient
});

// Listen for messages on the *:newuser channel
pubsub.on('*:newuser', function(channel, user) {
	console.log(channel); // prints "myservice:newuser"
	console.log(user);    // user is a json map as expected
});

// Publish an event "production:myservice:newuser" to the redis pubsub
pubsub.emit('myservice:newuser', { id:'a1b2c3', email:'[email protected]' });

API

.emit(channel, messages...) [publish]

var redis = require('redis-eventemitter');
var pubsub = redis({ prefix: 'production:', host: 'localhost', port: 6379 });

pubsub.emit('myservice:newuser', { id:'a1b2c3' });

.on(pattern, function(channel, messages...) { ... }) [subscribe]

var redis = require('redis-eventemitter');
var pubsub = redis({ scope: 'production:', host: 'localhost', port: 6379 });

pubsub.on('*:newuser', function(channel, message) {
	console.log(channel); // myservice:newuser
	console.log(message); // { id:'a1b2c3' }
});

.on('error', function(err) { ... }) [error handling]

To be able to handle errors (like when the redis server is down) .on('error', ...) should be used.

Note that this means that you can't listen for messages on the error channel.

.removeListener(pattern, listener)

Removes listener.

.removeAllListeners(pattern)

Removes all listeners.

Options

port

Port for the redis server.

host

Host for the redis server.

url

Url for the redis server.

auth_pass

Password for the redis server. Defaults to not being set.

prefix

A prefix that is added to the channel name, when publishing events to the redis pubsub. Useful for separating services or environments, e.g. production, development, and staging.

It is also removed when the listeners is invoked.

pub

Redis client instance used for publish.

sub

Redis client instance used for subscribe.

redis-eventemitter's People

Contributors

freeall avatar estliberitas avatar cayasso avatar mahen3d avatar mafintosh 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.