Skip to content

Node Plugin for adding rooms functionality to Primus

Notifications You must be signed in to change notification settings

Dshankar/primus-rooms

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Primus Rooms

Build Status NPM version

Node.JS module that adds room capabilities to a Primus server.

Instalation

npm install primus-rooms

Usage

On the Server

var Primus = require('primus');
var Rooms = require('primus-rooms');
var server = require('http').createServer();

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

// add rooms to Primus
primus.use('rooms', Rooms);

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

  spark.on('data', function(data) {

    data = data || {};
    var action = data.action;
    var room = data.room;

    // join a room
    if ('join' === action) {
      spark.join(room, function () {

        // send message to this client
        spark.write('you joined room ' + room);

        // send message to all clients except this one
        spark.room(room).write(spark.id + ' joined room ' + room);
      });
    }

    // leave a room
    if ('leave' === action) {
      spark.leave(room, function () {
        
        // send message to this client
        spark.write('you left room ' + room);
      });
    }

  });

});

server.listen(8080);

On the Client

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

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

  // Send request to join the news room
  primus.write({ action: 'join', room: 'news' });

  // Send request to leave the news room
  primus.write({ action: 'leave', room: 'news' });

  // print server message
  primus.on('data', function (message) {
    console.log(message);
  });

});

Client to client

Client

primus.write({ room: 'chat', msg: 'Hello some one' });

Server

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

  spark.on('data', function(data){
    var room = data.room;
    var message = data.msg;

    // check if spark is already in this room
    if (~spark.rooms().indexOf(room)) {
      send();
    } else {
    // join the room
    spark.join(room, function(){
      send();
    })
    }

    // send to all clients in the room
    function send() {
      spark.room(room).write(message);
    }
  })

});

API

primus.adapter(Adapter)

Set your own adapter for rooms, by default primus-rooms comes with its own memory adapter but its easy to provide a custom one.

// as argument
var primus = new Primus(url, { transformer: 'sockjs', adapter: myAdapter });
primus.use('rooms', Rooms);

// by calling the method
primus.adapter(new MyAdapter());

primus.join(spark, name, [fn])

Join client to a room, fn is optional callback.

primus.join(spark, 'room', fn);

primus.leave(spark, name, [fn])

Remove client from a specific room, fn is optional callback.

primus.leave(spark, 'room', fn);

primus.room(spark, name, [fn])

Target a specific room or rooms for broadcasting a message.

primus.room('room').write('hi');

in is an equivalent method to room:

primus.in('room').write('hi');

primus.room(room).write(message)

Send a message to a specific room.

primus.room('room').write('hi');

or to multiple rooms at once:

primus.room('sport news art').write('hi');

primus.room(name).except(ids);

Broadcast messages to clients in a room except to those especified.

primus.room('room').except('1386018854525$0 1386018854526$1').write('hi');

or pass an array:

var except = ['1386018854525$0', '1386018854526$1'];
primus.room('room').except(except).write('hi');

primus.room(room).clients([fn])

Get all client ids connected to a specific room. If no callback is passed the function will return synchronously the ids but please remember that NOT all adapters are guaranteed to be able to do this operation synchronously.

primus.room('room').clients(fn);

or synchronously if adapter supports it:

var clients = primus.room('room').clients();
console.log(clients);

primus.rooms([spark])

Get all active rooms on the server.

primus.rooms();

Get all rooms a specific spark is connected to.

primus.rooms(spark, fn);

primus.on('joinroom')

The joinroom event is emitted every time a spark has joined a room. First argument of the callback is the room and second argument is the spark.

primus.on('joinroom', function (room, spark) {
  console.log(spark.id + ' joined ' + room);
});

primus.on('leaveroom')

The leaveroom event is emitted every time a spark has left a room. First argument of the callback is the room and second argument is the spark.

primus.on('leaveroom', function (room, spark) {
  console.log(spark.id + ' left ' + room);
});

primus.on('leaveallrooms')

The leaveallrooms event is emitted every time the leaveAll method is called on a spark or when the end event is emitted on the client. First argument of the callback is an array with all rooms client joined.

primus.on('leaveallrooms', function (room, spark) {
  console.log(spark.id + ' leaving all rooms:', room);
});

primus.on('roomserror')

The roomserror event is emitted every time a spark encounter an error when joining or leaving a room. First argument of the callback is the error object and second argument is the spark.

primus.on('roomserror', function (error, spark) {
  console.log('room error from ' + spark.id, error);
});

spark.join(name, [fn])

Join client to a room, fn is optional callback.

spark.join('room');

Join multiple rooms at once.

spark.join('room1 room2 room3', fn);

spark.room(name, [fn])

Target a specific room.

spark.room('room').write('hi');
spark.room('room').clients(fn);

in is an equivalent method to room:

spark.in('room').write('hi');
spark.in('room').clients(fn);

spark.room(room).write(message)

Send a message to a specific room.

spark.room('room').write('hi');

spark.room(name).except(ids);

Broadcast messages to clients in a room except to those especified.

spark.room('room').except('1386018854525$0 1386018854526$1').write('hi');

or pass an array:

var except = ['1386018854525$0', '1386018854526$1'];
spark.room('room').except(except).write('hi');

spark.room(room).clients([fn])

Get all client ids connected to specific room. If no callback is passed the function will return synchronously the ids but please remember that NOT all adapters are guaranteed to be able to do this operation synchronously.

spark.room('room').clients(fn);

or synchronously if adapter supports it:

var clients = spark.room('room').clients();
console.log(clients);

spark.leave(name, [fn])

Leave a specific room, fn is optional callback.

spark.leave('room', fn);

Leave multiple rooms at once.

spark.leave('room1 room2 room3', fn);

spark.leaveAll()

Leave all rooms the client has joined.

spark.leaveAll();

spark.rooms()

Get all rooms client is connected to.

spark.rooms();

spark.isRoomEmpty([room])

Check to see if a room is empty.

spark.isRoomEmpty('sport');

or you can also use it like this:

spark.room('sport').isRoomEmpty();

spark.on('joinroom')

The joinroom event is emitted every time a spark has joined a room. First argument of the callback is the room.

spark.on('joinroom', function (room) {
  console.log(room);
});

spark.on('leaveroom')

The leaveroom event is emitted every time a spark has left a room. First argument of the callback is the room.

spark.on('leaveroom', function (room) {
  console.log(room);
});

spark.on('leaveallrooms')

The leaveallrooms event is emitted every time the leaveAll method is called on a spark or when the end event is emitted on the client. First argument of the callback is an array with all rooms client joined.

spark.on('leaveroom', function (room) {
  console.log(room);
});

spark.on('roomserror')

The roomserror event is emitted every time a spark encounter an error when joining or leaving a room. First argument of the callback is the error object.

spark.on('roomserror', function (error) {
  console.log(error);
});

Run tests

$ make test

Other plugins

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.

About

Node Plugin for adding rooms functionality to Primus

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%