primus-emitter

Simple emitter wrapper for Primus

npm install primus-emitter
99 downloads in the last day
742 downloads in the last week
3 054 downloads in the last month

Primus Emitter

Build Status NPM version

Node.JS module that adds emitter capabilities to Primus.

Compatibility

primus-emitter@3.x.x is now compatible with primus@2.x.x series, if for some reason you need to continue using an old version of primus then you can always go back and install a previous version of primus-emitter like so:

$ npm install primus-emitter@2.0.5

Instalation

$ npm install primus-emitter

Usage

On the Server

var Primus = require('primus');
var Emitter = require('primus-emitter');
var server = require('http').createServer();

// primus instance
var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });

// add emitter to Primus
primus.use('emitter', Emitter);

primus.on('connection', function (spark) {

  // emit hi event
  spark.send('hi', 'good morning');

  // emit to news with ack
  spark.send('news', 'good morning', function (data) {
    console.log(data); // => 'by client'
  });

  // receive incoming sport messages
  spark.on('sport', function (data) {
    console.log('sport', data); // => ping-pong
  });

});

server.listen(8080);

On the Client

var primus = Primus.connect('ws://localhost:8080');

primus.on('open', function () {

  // receive incoming hi msgs
  primus.on('hi', function (data) {
    console.log(data); // => good morning
  });

  // respond ack to server
  primus.on('news', function (data, fn) {
    fn('by client')
  });

  // send message to server
  primus.send('sport', 'ping-pong');

});

API

spark#send(event, ..., [fn])

Send an event to server to client or client to server.

spark.send('news', 'hi', fn);

spark#on(event, fn)

Listen to incoming events.

spark.on('news', fn);

Run tests

$ make test

Other plugins

License

(The MIT License)

Copyright (c) 2013 Jonathan Brumley <cayasso@gmail.com>

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.

npm loves you