primus.io

Primus.IO makes working with Primus a little slicker.

Primus.IO

Primus.IO makes working with Primus a little slicker, it adds some high-level features like:

  • Emit-style with send() w/ arguments.
  • Client & server side "ack" callbacks.
  • Multiplexing via channels.
  • Rooms.
  • Serves /primus.io.js.

Primus.IO combines the core Primus with primus-rooms, primus-emitter and primus-multiplex plugins to provide an easy and still powerfull way of developing real time applications.

For more details on options or additional methods please check each individual module README file and test cases.

$ npm install primus.io
var Primus = require('primus.io');
var server = require('http').createServer();
 
var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
primus.on('connection', function (spark) {
 
  // listen to hi events 
  spark.on('hi', function (msg) {
 
    console.log(msg); //-> hello world 
 
    // send back the hello to client 
    spark.send('hello', 'hello from the server');
 
  });
 
});
 
server.listen(8080);

If using in the browser just:

<script src="/primus/primus.io.js"></script>

Then create your client Primus instance like this:

var socket = Primus.connect('ws://localhost:8080');
 
socket.on('open', function () {
 
  // Send request to join the news room 
  socket.send('hi', 'hello world');
 
  // listen to hello events 
  socket.on('hello', function (msg) {
 
    console.log(msg); //-> hello from the server 
 
  });
 
});
 

If in NodeJS using the same Primus instance that created the server then do:

// create socket instance 
var socket = new primus.Socket('ws://localhost:8080');
 
socket.on('open', function () {
 
  // Send request to join the news room 
  socket.send('hi', 'hello world');
 
  // listen to hello events 
  socket.on('hello', function (msg) {
 
    console.log(msg); //-> hello from the server 
 
  });
 
});

If using a different instance of NodeJS then do this:

// create a socket 
var Socket = require('primus.io').createSocket({ transformer: 'websockets' });
 
// get socket instance 
var socket = new Socket('ws://localhost:8080');
 
socket.on('open', function () {
 
  // Send request to join the news room 
  socket.send('hi', 'hello world');
 
  // listen to hello events 
  socket.on('hello', function (msg) {
 
    console.log(msg); //-> hello from the server 
 
  });
 
});

Check the examples for more use cases.

  var Primus = require('primus.io')
    , http = require('http')
    , fs = require('fs');
 
  // serve index.html 
  var server = http.createServer(function server(reqres) {
    res.setHeader('Content-Type', 'text/html');
    fs.createReadStream(__dirname + '/index.html').pipe(res);
  });
 
  // Primus server 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  primus.on('connection', function (spark) {
    spark.send('news', { hello: 'world' });
    spark.on('my other event', function (data) {
      console.log(data);
    });
  });
 
  server.listen(8080);
  var primus = new Primus('http://localhost:8080/');
 
  primus.on('news', function (data) {
    console.log(data);
    primus.send('my other event', { my: 'data' });
  });

Express requires that you instantiate a http.Server to attach socket.io to first:

  var express = require('express')
    , Primus = require('primus.io')
    , http = require('http')
    , app = express()
    , server = http.createServer(app);
 
  // Primus server 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  primus.on('connection', function (spark) {
    spark.send('news', { hello: 'world' });
    spark.on('my other event', function (data) {
      console.log(data);
    });
  });
 
  // serve index.html 
  app.get('/', function (reqres) {
    res.sendfile(__dirname + '/index.html');
  });
 
  server.listen(8080);
  var primus = new Primus('http://localhost:8080/');
 
  primus.on('news', function (data) {
    console.log(data);
    primus.send('my other event', { my: 'data' });
  });

Primus.IO allows you to emit and receive custom events:

  var Primus = require('primus.io')
    , server = require('http').Server();
 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  primus.on('connection', function (spark) {
 
    spark.send('welcome', 'welcome to the server');
 
    spark.on('private message', function (frommsg) {
      console.log('I received a msg by ', from, ' saying ', msg);
    });
 
  });
 
  server.listen(8080);
  var primus = new Primus('http://localhost:8080/');
 
  primus.on('welcome', function (msg) {
    primus.send('private message', 'Bob', 'hi!');
  });

Check for more documentation on event emitting here primus-emitter.

Channels provides the benefit of multiplexing a single connection.

  var Primus = require('primus.io')
    , server = require('http').Server();
 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  var chat = primus.channel('chat');
  var news = primus.channel('news');
 
  chat.on('connection', function (spark) {
    spark.send('chat', 'welcome to this chat');
  });
 
  news.on('connection', function (socket) {
      socket.send('news', { news: 'item' });
  });
 
  server.listen(8080);
  var primus = new Primus('http://localhost:8080/')
    , chat = primus.channel('chat')
    , news = primus.channel('news');
 
  chat.on('chat', function (msg) {
    console.log(msg); //-> welcome to this chat 
  });
 
  news.on('news', function (data) {
    console.log(data.news); //-> item 
  });

Checkout this post for more deep understanding of channels and why it's implemented like this.

Also check out for more documentation on multiplexing here primus-multiplex.

To get a callback when the server or client confirmed the message reception, simply pass a function as the last parameter of .send.

  var Primus = require('primus.io')
    , server = require('http').Server();
 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  primus.on('connection', function (spark) {
    spark.on('chat', function (namefn) {
    console.log(name); //-> Bob 
    fn('woot');
 
    spark.send('What is your name', function (name) {
      console.log(name); //-> My name is Ann 
    });
  });
 
});
  var primus = new Primus('http://localhost:8080/');
 
  primus.on('open', function () {
    primus.send('chat', 'Bob', function (msg) {
      console.log(msg); //-> woot 
    });
 
    primus.on('What is your name', function (fn) {
      fn('My name is Ann')
    });
  });

To broadcast a message to all connected clients simple use the primus.write method. The same apply for channels.

  var Primus = require('primus.io')
    , server = require('http').Server();
 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  primus.write('Some data');
 
});
  var primus = new Primus('http://localhost:8080/');
 
  primus.on('data', function (data) {
    console.log(data); //-> Some data 
  });

You can also broadcast messages to all clients by emitting events using the primus.send method. The same apply for channels.

  var Primus = require('primus.io')
    , server = require('http').Server();
 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  primus.send('news', 'Some data');
 
});
  var primus = new Primus('http://localhost:8080/');
 
  primus.on('news', function (data) {
    console.log(data); //-> Some data 
  });

You can also use the primus.forEach method to iterate over all current connections.

  var Primus = require('primus.io')
    , server = require('http').Server();
 
  var primus = new Primus(server, { transformer: 'websockets', parser: 'JSON' });
 
  primus.forEach(function (sparkidconnections) {
    if (spark.query.foo !== 'bar') return;
 
    spark.write('message');
  });
 
});

Check out more information on broadcasting with Primus.

var Primus = require('primus.io');
var server = require('http').createServer();
 
// primus instance 
var primus = new Primus(server, { transformer: 'websockets' });
 
primus.on('connection', function (spark) {
 
  spark.on('join', function (room) {
    spark.join(room, function () {
 
      // send message to this client 
      spark.send('sport', 'you joined room ' + room);
 
      // send message to all clients except this one 
      spark.room(room).except(spark.id).send('sport', spark.id + ' joined room ' + room);
    });
  });
 
  spark.on('leave', function (room) {
    spark.leave(room, function () {
 
      // send message to this client 
      spark.send('sport', 'you left room ' + room);
    });
  });
 
});
 
server.listen(8080);
var primus = Primus.connect('ws://localhost:8080');
 
primus.on('open', function () {
 
  // Send request to join the sport room 
  primus.send('join', 'sport');
 
  // Then later send request to leave the sport room 
  primus.send('leave', 'sport');
 
  // print server message 
  primus.on('sport', function (message) {
 
    console.log(message);
 
    // First output is 
    //-> you joined room sport 
 
    // Then later 
    //-> you left room sport 
 
  });
 
});
 

You can check for more documentation on rooms here primus-rooms.

$ make test
  • To Arnout Kazemier 3rdEden for the awesome idea of building Primus.

(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.