Skip to content

Latest commit

 

History

History
512 lines (337 loc) · 11.7 KB

README.md

File metadata and controls

512 lines (337 loc) · 11.7 KB

Primus.IO

Build Status NPM version

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.

Installation

$ npm install primus.io

Quick Start

On the Server

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);

On the Client

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.

How to use

Using with Node HTTP server

Server

  var Primus = require('primus.io')
    , http = require('http')
    , fs = require('fs');

  // serve index.html
  var server = http.createServer(function server(req, res) {
    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);

Client

  var primus = new Primus('http://localhost:8080/');

  primus.on('news', function (data) {
    console.log(data);
    primus.send('my other event', { my: 'data' });
  });

Using with Express

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

Server

  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 (req, res) {
    res.sendfile(__dirname + '/index.html');
  });

  server.listen(8080);

Client

  var primus = new Primus('http://localhost:8080/');

  primus.on('news', function (data) {
    console.log(data);
    primus.send('my other event', { my: 'data' });
  });

Sending and receiving events.

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

Server

  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 (from, msg) {
      console.log('I received a msg by ', from, ' saying ', msg);
    });

  });

  server.listen(8080);

Client

  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.

Using channels (or known as namespaces).

Channels provides the benefit of multiplexing a single connection.

Server

  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);

Client

  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.

Acknowledgements

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

Server

  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 (name, fn) {
    console.log(name); //-> Bob
    fn('woot');

    spark.send('What is your name', function (name) {
      console.log(name); //-> My name is Ann
    });
  });

});

Client

  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')
    });
  });

Broadcasting messages (server side).

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

Server

  var Primus = require('primus.io')
    , server = require('http').Server();

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

  primus.write('Some data');

});

Client

  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.

Server

  var Primus = require('primus.io')
    , server = require('http').Server();

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

  primus.send('news', 'Some data');

});

Client

  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.

Server

  var Primus = require('primus.io')
    , server = require('http').Server();

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

  primus.forEach(function (spark, id, connections) {
    if (spark.query.foo !== 'bar') return;

    spark.write('message');
  });

});

Check out more information on broadcasting with Primus.

Rooms

Server

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);

Client

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.

Run tests

$ make test

Credits

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

License

(The MIT License)

Copyright (c) 2013 Jonathan Brumley <[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.