Primus Rooms
Node.JS module that adds room capabilities to a Primus server.
Installation
npm install primus-rooms
Usage
On the Server
var Primus = ;var Rooms = ;var server = ; // primus instancevar primus = server transformer: 'websockets' ; // add rooms to Primusprimus; primus; server;
On the Client
var primus = Primus; primus;
Client to client
Client
primus;
Server
primus;
Wildcard support
spark1;spark2;spark3;
Then later in your code send a message as normal like this:
// All clients of the rooms above should receive 'hello'.primus;
All clients that joined rooms user:*
, user:*:*
, user:*:*:123
should receive the hello
message.
// All clients of the rooms above should receive 'hello'.spark1; spark2; spark3;
Disabling wildcard
Wildcard is enabled by default, to disable it just pass { wildcard: false }
in the rooms options when instantiating primus.
var Primus = ;var Rooms = ;var server = ; // disabling wildcardvar primus = server rooms: wildcard: false ; // add rooms to Primusprimus;
For more examples on how to use wildcard
check the wildcard tests.
API
primus.adapter
Set your own adapter
for rooms, by default primus-rooms
comes
with its own memory
adapter primus-rooms-adapter but its easy to provide a custom one.
// as argumentvar primus = url transformer: 'sockjs' rooms: adapter: myAdapter ;primus; // by setting the propertyprimusadapter = ;
For more information on how to create your own custom adapter check out the documentation of primus-rooms-adapter.
primus.join(sparks, name, [fn])
Join multiple sparks to a room
or multiple rooms, fn
is optional callback.
primus; // to multiple roomsprimus;
Join multiple sparks to a room
or multiple rooms by passing spark ids.
primus; // to multiple roomsprimus;
You can also mix ids with instances in the array:
primus;
You can also pass a single spark instance or id to join a room or multiple rooms:
primus;
This is also equivalent:
primus;
primus.leave(sparks, name, [fn])
Remove multiple sparks from a room
or multiple rooms, fn
is optional callback.
primus; // multiple roomsprimus;
Remove multiple sparks from a room
or multiple rooms by passing spark ids.
primus; // multiple roomsprimus;
You can also mix ids with instances in the array:
primus;
You can also pass a single spark instance or id to leave a room or multiple rooms:
primus;
This is also equivalent:
primus;
primus.room(room)
Target a specific room
or rooms for broadcasting a message.
primus;
in
is an equivalent method to room
:
primus;
primus.room(room).write(message)
Send a message to a specific room
.
primus;
or to multiple rooms at once:
primus;
primus.room(name).except(ids);
Broadcast messages to clients in a room except to those especified.
primus;
or pass an array:
var except = '1386018854525$0' '1386018854526$1';primus;
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;
or synchronously if adapter supports it:
var clients = primus;console;
primus.room(room).empty()
Remove all clients from a room
or multiple room
s.
primus; // orprimus;
or multiple rooms at the same time:
primus; // orprimus;
primus.room(name).transform(fn);
primus;
This also work asynchronously:
primus;
primus.rooms([spark], [fn])
Get all active rooms on the server.
primus;
Get all rooms a specific spark is connected to.
primus;
You can also use the spark id:
primus; // orprimus;
primus.isRoomEmpty(room, [fn])
Check if a room
is empty, fn
is optional callback.
primus;
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;
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;
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;
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;
spark.join(name, [fn])
Join client to a room
, fn
is optional callback.
spark;
Join multiple rooms at once.
spark;
spark.room(room)
Target a specific room
.
spark;spark;
in
is an equivalent method to room
:
spark;spark;
spark.room(room).write(message)
Send a message to a specific room
.
spark;
spark.room(name).except(ids);
Broadcast messages to clients in a room except to those specified.
spark;
or pass an array:
var except = '1386018854525$0' '1386018854526$1';spark;
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;
or synchronously if adapter supports it:
var clients = spark;console;
spark.room(name).transform(fn);
spark;
This also work asynchronously:
spark;
spark.leave(name, [fn])
Leave a specific room
, fn
is optional callback.
spark;
Leave multiple rooms at once.
spark;
spark.leaveAll([fn])
Leave all rooms the client has joined, fn
is optional callback.
spark;
spark.rooms()
Get all rooms client is connected to.
spark;
spark.isRoomEmpty(name, [fn])
Check if a room
is empty, fn
is optional callback.
spark;
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;
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;
spark.on('leaveallrooms')
The leaveallrooms
event is emitted every time the leaveAll method
is called on a spark or when the connection gets closed.
First argument of the callback is an array with all rooms
client joined.
spark;
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;
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.