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.
Installation
$ npm install primus.io
Quick Start
On the Server
var Primus = ;var server = ; var primus = server; primus; server;
On the Client
If using in the browser just:
Then create your client Primus
instance like this:
var socket = Primus; socket;
If in NodeJS using the same Primus
instance that created the server then do:
// create socket instancevar socket = 'http://localhost:8080'; socket;
If using a different instance of NodeJS then do this:
// create a socketvar Socket = ; // get socket instancevar socket = 'http://localhost:8080'; socket;
Check the examples for more use cases.
How to use
Using with Node HTTP server
Server
var Primus = http = fs = ; // serve index.htmlvar server = http; // Primus servervar primus = server; primus; server;
Client
var primus = 'http://localhost:8080/'; primus;
Using with Express
Express requires that you instantiate a http.Server
first:
Server
var express = Primus = http = app = server = http; // Primus servervar primus = server; primus; // serve index.htmlapp; server;
Client
var primus = 'http://localhost:8080/'; primus;
Sending and receiving events.
Primus.IO allows you to emit and receive custom events:
Server
var Primus = server = ; var primus = server; primus; server;
Client
var primus = 'http://localhost:8080/'; primus;
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 = server = ; var primus = server; var chat = primus;var news = primus; chat; news; server;
Client
var primus = 'http://localhost:8080/' chat = primus news = primus; chat; news;
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 = server = ; var primus = server; primus
Client
var primus = 'http://localhost:8080/'; primus;
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 = server = ; var primus = server; primus;
Client
var primus = 'http://localhost:8080/'; primus;
You can also broadcast messages to all clients by emitting events using the
primus.send
method. The same apply for channels.
Server
var Primus = server = ; var primus = server; primus;
Client
var primus = 'http://localhost:8080/'; primus;
You can also use the primus.forEach
method to iterate over all current
connections.
Server
var Primus = server = ; var primus = server; primus;
Check out more information on broadcasting with Primus.
Rooms
Server
var Primus = ;var server = ; // primus instancevar primus = server; primus; server;
Client
var primus = Primus; primus;
You can check for more documentation on rooms here primus-rooms.
Run tests
$ make test
Credits
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.