• Stars
    star
    280
  • Rank 147,492 (Top 3 %)
  • Language
    JavaScript
  • Created almost 12 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple pubsub for node using Redis

NRP (Node Redis Pubsub)

This library is now mainly maintained by @rangermauve and @narcisoguillen

Simple pubsub for node using Redis. Why use NRP instead of Node's EventEmitter? It is useful when your Node application needs to share data with other applications. In that case EventEmitter will not help you, you need an external pubsub provider. Redis is pretty good at this, but its pubsub API is strange. So you use this wrapper.

Install and test

$ npm install node-redis-pubsub      # Install locally
$ npm install -g node-redis-pubsub   # Install globally
$
$ make test   # test (devDependencies need to be installed and a Redis server up)

Usage

Setup

for a trusted environment where Redis runs locally, unprotected on a port blocked by firewall.

var NRP    = require('node-redis-pubsub');
var config = {
  port  : 6379  , // Port of your locally running Redis server
  scope : 'demo'  // Use a scope to prevent two NRPs from sharing messages
};

var nrp = new NRP(config); // This is the NRP client

for a remote Redis server

var NRP = require('node-redis-pubsub');

var config = {
  port: 1234                        , // Port of your remote Redis server
  host: 'path.to.remote.redis.host' , // Redis server host, defaults to 127.0.0.1
  auth: 'password'                  , // Password
  scope: 'demo'                       // Use a scope to prevent two NRPs from sharing messages
};

var nrp = new NRP(config); // This is the NRP client

heroku and other services provide you with an environment variable REDIS_URL

var NRP = require('node-redis-pubsub');
var url = process.env.REDIS_URL;

var config = {
    url: url
};

var nrp = new NRP(config); // This is the NRP client

reuse existing redis pub and sub connections

var NRP = require('node-redis-pubsub');

var redisPub = redis.createClient();
var redisSub = redis.createClient();

var config = {
  emitter: redisPub,                      // Pass in an existing redis connection that should be used for pub
  receiver: redisSub,                     // Pass in an existing redis connection that should be used for sub
}

var nrp = new NRP(config); // This is the NRP client

Simple pubsub

nrp.on('say hello', function(data){
  console.log('Hello ' + data.name);
});

nrp.emit('say hello', { name: 'Louis' });   // Outputs 'Hello Louis'

// You can use patterns to capture all messages of a certain type
// The matched channel is given as a second parameter to the callback
nrp.on('city:*', (data, channel) => {
  console.log(data.city + ' is great');
});

nrp.emit('city:hello' , { city: 'Paris' });         // Outputs 'Paris is great'
nrp.emit('city:yeah'  , { city: 'San Francisco' }); // Outputs 'San Francisco is great'

Do something after subscribe finishes

nrp.on('mydata:sync', function(myData) {
  console.log(myData);
}, function() {
  nrp.emit('mydata:requestsync'); // request a sync of the data after the handler is registered, so there are no race conditions
});

Unsubscribe

var unsubscribe = nrp.on('say hello', function(data){
  // Never called
});

unsubscribe([Callback]);

Shut down connections

// Safely (connections will be closed properly once all commands are sent)
nrp.quit();

// Dangerously (connections will be immediately terminated)
nrp.end();

Listen for errors

nrp.on("error", function(){
  // Handle errors here
});

License

(The MIT License)

Copyright (c) 2012 tldr.io <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
JavaScript
13,357
star
2

mongo-edit

A simple REST gui for MongoDB
JavaScript
228
star
3

node-binary-search-tree

Self-balancing binary search tree for Node.js (uses AVL tree)
JavaScript
146
star
4

nedb-to-mongodb

Utility to transfer all your data in a nedb database in a MongoDB collection
JavaScript
85
star
5

nedb-logger

A logger that output message to a file in an nedb-readable format with minimal memory footprint
JavaScript
50
star
6

braindead-ci

JavaScript
48
star
7

connect-nedb-session

NeDB-backed session store for the Connect/Express session middleware
JavaScript
37
star
8

express-group-handlers

Apply a middleware only to a group of route handlers in Express
JavaScript
32
star
9

h4e

Hogan for Express, with support for partials
JavaScript
18
star
10

trello-capture

Chrome extension to take a (partial) screenshot of a webpage and create a new Trello card with it in it
JavaScript
15
star
11

nedb-server

HTTP interface for a NeDB database
JavaScript
12
star
12

M-command

Chrome plugin to get the same behaviour as vim's M command
JavaScript
12
star
13

exec-time

See how much time every step of a node script takes
JavaScript
10
star
14

express-nedb-session

Nedb-backed session store for Express 4 session middleware
JavaScript
9
star
15

taffydb-benchmark

Benchmark of TaffyDB on basic CRUD operations. The benchmark code is the same as the one used for NeDB.
JavaScript
7
star
16

url-shortener

A basic url shortener
JavaScript
5
star
17

raspi-media-center

Media center based on Rasberry Pi, remotely controllable by smartphone through HTTP server
JavaScript
5
star
18

node-pbkdf2

Wrapper to hash and check password with crypto's built-in pbkdf2, abstracting the API change between node v0.8 and v0.10
JavaScript
5
star
19

NodejsStarterKit

Starter kit I use on my side projects to create websites/api with nodejs and express
JavaScript
4
star
20

raycasting

Minimal raycasting 3D engine
JavaScript
3
star
21

Stanford-Classes

My project files for Stanford's online classes
Java
3
star
22

lm-heatmap

Local Motion heatmaps
JavaScript
3
star
23

dotfiles

My dotfiles
Shell
3
star
24

tetris

A small DOM tetris game
JavaScript
2
star
25

data-graphs

Small chart library for use with Local Motion's data reports
JavaScript
2
star
26

peaceful-internet

Remove unneeded attention grabbing from the internet
JavaScript
2
star
27

btc-bot

JavaScript
2
star
28

pipedrive-plus

Set of tools to enhance Pipedrive
JavaScript
2
star
29

snake

A good old snake
JavaScript
1
star
30

jeu-ines

Q&A game for my sister's marriage
JavaScript
1
star
31

raytracing

A minimalist raytracing engine
Python
1
star
32

fundor

Self-updating list of startup that raise money
JavaScript
1
star
33

ml_example

Super basic example to show how machine learning works
JavaScript
1
star
34

mariokart-web-client

JavaScript
1
star
35

advent-of-code

Hopefully I have the time to do it!
Python
1
star
36

gop

Multiplayer real time Go playing server
JavaScript
1
star