growing steady star it on http://github.com/primus/primus 553 5 developers with commit access make 2 great pull requests, and you’ll get full github access forks which i regularly check for commits that people “forget” to contribute 43
'use strict';! ! var Primus = require("primus")! , server = require("http").createServer(fn)! , primus = new Primus(server, { transformer:"ws" });! ! primus.on("connection", function connection(spark) {! console.log("connection received", spark.id);! spark.write("ohai");! ! spark.on("data", function data(msg) {! console.log("received", msg);! });! });! ! server.listen(8080); CODE EXAMPLE: PRIMUS SERVER
require(“primus/transformer").extend({! server: function () {! // This is only exposed and ran on the server.! },! ! client: function () {! // This is stringified end send/stored in the client.! // Can be ran on the server, if used through Node.js! },! ! // Optional library for the front-end, assumes globals! library: fs.readFileSync(__dirname +"./yourclientlib.js")! }); CODE EXAMPLE: PRIMUS SERVER 1 line of code
socket.io multiple transports cross domain invested with bugs poorly / not maintained / left for dead no message order guarantee dies behind firewall/virusscanners
supports multiple transports cross domain upgrade instead of downgrade works behind firewalls & virusscanners not well tested no message order guarantee
browserchannel multiple transports client maintained by google message order guaranteed works behind firewalls & virusscanners not cross domain no websocket support coffeescript on the server ._.
sockjs multiple transports (tons of them) cross domain poor error handling no query string allowed for connect argh, more coffeescript connection delay with firewalls
websockets (ws) super damned fast supports binary cross domain i’m the only somewhat maintainer no fallbacks or downgrading broken by firewalls/virusscanners
{! encoder: function (data, fn) {! // encode data to a string.! },! ! decoder: function (data, fn) {! // decode data to an object! },! ! // Optional library for the front-end, assumes globals! library: fs.readFileSync(__dirname +"./yourclientlib.js")! }; CODE EXAMPLE: PRIMUS SERVER custom encoders
primus.transform('incoming', function (packet) {! // This would transform all incoming messages to foo;! packet.data = 'foo';! });! ! primus.transform('outgoing', function (packet) {! // This would transform all outgoing messages to foo;! packet.data = 'foo';! }); CODE EXAMPLE: PRIMUS SERVER & CLIENT message transformers
// send message to all users! ! primus.forEach(function (spark) {! // Or iterate over all connections, select the once you! // want and only write to those! ! spark.write("message");! }); CODE EXAMPLE: PRIMUS SERVER
on node var primus = new Primus(server)! , Socket = primus.Socket;! ! var client = new Socket(“http://localhost:8080”);! ! // or if you want to connect to a remote server:! var primus = require(“primus”).createSocket({/* options */}); CODE EXAMPLE: PRIMUS SERVER
The long awaited Socket.IO 1.0 release with Primus:! ! var server = require("http").createServer(fn)! , primus = new Primus(server, { transformer:"engine.io" });! ! primus.use(“emitter","primus-emitter")! .use(“multiplex”, require(“primus-multiplex”))! .use(“primus-emitter”, "primus-rooms"); CODE EXAMPLE: PRIMUS SERVER
{! server: function () {! // This is only exposed and ran on the server.! },! ! client: function () {! // This is stringified end send/stored in the client.! // Can be ran on the server, if used through Node.js! },! ! // Optional library for the front-end, assumes globals! library: fs.readFileSync(__dirname +"./yourclientlib.js")! }; CODE EXAMPLE: PRIMUS SERVER plugins
more client works in node.js without changes authorization handling ip address resolving even behind proxies pathname exposure library generation smart server closing which boots connections full header access access to raw package data event based logger