Simple multiplexing for Primus
Node.JS module that adds mutiplexing to Primus.
email@example.com is now compatible with firstname.lastname@example.org series, if for some reason you need to continue using an old version of
primus then you can always go back and install a previous version of
primus-multiplex like so:
$ npm install email@example.com
$ npm install primus-multiplex
var Primus = require'primus';var multiplex = require'primus-multiplex';var server = require'http'createServer;// primus instancevar primus = server transformer: 'sockjs' parser: 'JSON' ;// add multiplex to Primusprimususe'multiplex' multiplex;var news = primuschannel'news';newson'connection'sparkwrite'hi from the news channel';sparkon'data'sparkwritedata;;;var sport = primuschannel'sport';sporton'connection'sparkwrite'hi from the sport channel';sparkon'data'sparkwritedata;;;serverlisten8080;
var primus = Primusconnect'ws://localhost:8080';// Connect to channelsvar news = primuschannel'news';var sport = primuschannel'sport';// Send messagenewswrite'hi news channel';sportwrite'hi sport channel';// Receive messagenewson'data'console.logmsg;;sporton'data'console.logmsg;;
Create a new channel on the server.
var news = primuschannel'news';newson'connection' fn;
Broadcast a message to all connected
Sparks in the channel.
Iterare over all
Sparks in a channel. This could also be used
for broadcasting to specific
Destroy the channel removing all 'Sparks' and event listeners.
This will emit a
Triggers when the destroy method is called.
newson'connection'newsdestroy;;newson'close'console.log'channel was destroyed';;
End the connection.
Send a message to the server.
Disconnect from a channel.
var news = primuschannel'news';newsend;
data from the server form the corresponding
newson'data'console.log'Received message from news channel' msg;;
There are two useful events that will be triggered on the main primus
Spark object, these events can be very usefull for handling dynamic subscriptions, subscription notifications, etc.
Triggers when a connection is subscribed to a channel. Callback will return with
primuson'connection'sparkon'subscribe'console.log'spark %s subscribed to channel %s' channelSparkid channelname;// do stuff with the channel spark;;
Triggers when connection is unsubscribed from a channel. Callback will return the
primuson'connection'sparkon'unsubscribe'console.log'spark %s unsubscribed from channel %s' channelSparkid channelname;// channel spark is about to die;;
Each message consists of an array of four parts:
There are three valid message types:
0) send a message with
1) subscribe to a given
2) unsubscribe from a
topic identifies a channel registered on the server side.
id represent a unique connection identifier generated on the client side.
Each request to subscribe to a topic from a given client has a unique id. This makes it possible for a single client to open multiple independent channel connection to a single server-side service.
Invalid messages are simply ignored.
It's important to notice that the namespace is shared between both parties and it is not a good idea to use the same topic names on the client and on the server side. Both parties may express a will to unsubscribe itself or other party from a topic.
$ make test
This library was inspire by this great post:
PrimusMultiplex is compatible with the following plugins, check the examples to see more.
(The MIT License)
Copyright (c) 2013 Jonathan Brumley <firstname.lastname@example.org>
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.