Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add multiplexing to Primus

NotificationsYou must be signed in to change notification settings

cayasso/primus-multiplex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusNPM version

Node.JS module that adds mutiplexing toPrimus.

Installation

$ npm install primus-multiplex

Usage

On the Server

varPrimus=require('primus');varmultiplex=require('primus-multiplex');varserver=require('http').createServer();// primus instancevarprimus=newPrimus(server,{transformer:'sockjs',parser:'JSON'});// add multiplex to Primusprimus.plugin('multiplex',multiplex);varnews=primus.channel('news');news.on('connection',function(spark){spark.write('hi from the news channel');spark.on('data',function(data){spark.write(data);});});varsport=primus.channel('sport');sport.on('connection',function(spark){spark.write('hi from the sport channel');spark.on('data',function(data){spark.write(data);});});server.listen(8080);

On the Client

varprimus=Primus.connect('ws://localhost:8080');// Connect to channelsvarnews=primus.channel('news');varsport=primus.channel('sport');// Send messagenews.write('hi news channel');sport.write('hi sport channel');// Receive messagenews.on('data',function(msg){console.log(msg);});sport.on('data',function(msg){console.log(msg);});

API

Server

primus.channel(name)

Create a new channel on the server.

varnews=primus.channel('news');news.on('connection',fn);

channel.write(message)

Broadcast a message to all connectedSparks in the channel.

news.write(message);

channel.forEach(fn)

Iterare over allSparks in a channel. This could also be usedfor broadcasting to specificSparks.

news.forEach(function(spark,id,connections){spark.write('message');});

channel.destroy()

Destroy the channel removing all 'Sparks' and event listeners.This will emit aclose event.

news.on('connection',function(spark){news.destroy();});

channel.on('close', fn)

Triggers when the destroy method is called.

news.on('connection',function(spark){news.destroy();});news.on('close',function(){console.log('channel was destroyed');});

spark.end([fn])

End the connection.

news.on('connection',function(spark){spark.end(fn);});

Client

channel.write(message)

Send a message to the server.

news.write('hi server');

channel.end()

Disconnect from a channel.

varnews=primus.channel('news');news.end();

channel.on('data', fn)

Receivedata from the server from the correspondingchannel.

news.on('data',function(msg){console.log('Received message from news channel',msg);});

Events on main Spark (Server)

There are two useful events that will be triggered on the main primusSpark object, that can be very usefull for handling dynamic subscriptions, subscription notifications, etc.

spark.on('subscribe', fn)

Triggers when a connection is subscribed to a channel. Callback will return withchannel objectand itsspark object.

primus.on('connection',function(spark){spark.on('subscribe',function(channel,channelSpark){console.log('spark %s subscribed to channel %s',channelSpark.id,channel.name);// do stuff with the channel spark});});

spark.on('unsubscribe', fn)

Triggers when connection is unsubscribed from a channel. Callback will return thechannel objectand itsspark object.

primus.on('connection',function(spark){spark.on('unsubscribe',function(channel,channelSpark){console.log('spark %s unsubscribed from channel %s',channelSpark.id,channel.name);// channel spark is about to die});});

Protocol

Each message consists of an array of four parts:type (Number),id (String),topic (String), andpayload (Mixed).

There are three valid message types:

  • Packet#MESSAGE (0) send a message withpayload on atopic.
  • Packet#SUBSCRIBE (1) subscribe to a giventopic.
  • Packet#UNSUBSCRIBE (2) unsubscribe from atopic.

Thetopic identifies a channel registered on the server side.Theid 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 independentchannel connection to a single server-side service.

Invalid messages are simply ignored.

It's important to notice that the namespace is shared between bothparties and it is not a good idea to use the same topic names on theclient and on the server side. Both parties may express a will tounsubscribe itself or other party from a topic.

Run tests

$ maketest

Inspiration

This library was inspire by this great post:

Other plugins

PrimusMultiplex is compatible with the following plugins, check theexamples to see more.

License

(The MIT License)

Copyright (c) 2013 Jonathan Brumley <cayasso@gmail.com>

Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the'Software'), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:

The above copyright notice and this permission notice shall beincluded 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 OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANYCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THESOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Add multiplexing to Primus

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp