Skip to content

Latest commit

 

History

History
65 lines (46 loc) · 1.6 KB

README.md

File metadata and controls

65 lines (46 loc) · 1.6 KB

Notify

Build Status Coverage Status

A pub/sub plugin for Primus.

Installation

npm install mlebrun/notify --save

Usage

Server

To use the pub/sub functionality, simply use Notify after Primus Emit and Primus Rooms.

var Notify = require('notify');
var Subscriber = require('notify-redis-subscriber');

var config = {
  port: 34000,
  transformer: 'engine.io',
};

// pass in subscriber via config
config.subscriber = new Subscriber();

var primus = new Primus(server, config);
primus.use('notify', Notify);

When a subscriber a receives an event from that key, it will forward that event to the client. The event that is fired should be structured as so:

subscriber.emit('message', 'my-key', { "type": "some-event", "data": { "...any extra event data" } });

Client

From the client we just emit keys we want to subscribe to.

var socket = new Primus();

socket.on('open', function() {
  // subscribe to a key
  this.emit('subscribe', 'my-key');
});

// listen for specific events from that key
socket.on('some-event', function() {
  console.log( 'Hi!' );
});

All that is needed is to select a pre-built Subscriber or provide your own.

Writing a custom adapter is easy. This one wraps Redis.