12. user agent sniïŹ
if (!
// Target safari browsers!
$.browser.safari!
!
// Not chrome!
&& !$.browser.chrome!
!
// And correct WebKit version!
&& parseFloat($.browser.version, 0) < 534.54!
) {!
// Donât use WebSockets!
return;!
}
13. Writing to a closed WebSocket can
cause a full browser crash
14. write crash
var ws = new WebSocket("wss://localhost:8080/");!
!
ws.onmessage = function message(event) {!
// Wrap sends in a setTimeout out to allow the!
// readyState to be correctly set to closed. But!
// Only have this delay on mobile devices!
if (mobile) return setTimeout(function () {!
ws.send("Sup Oslo");!
}, 0);!
!
ws.send("Sup Oslo");!
};
16. connection end
$('body').keydown(function (e) {!
// make sure that you capture the `esc` key and!
// prevent it's default action from happening!
if (e.which === 27) e.preventDefault();!
});
17. Firefox can create ghost connections
when you connect during
ws.onclose.
18. Donât use self signed SSL
certiïŹcates, not for development and
not in production
19. 4G, 3G, LTE, mobile providers WTF
ARE YOU DOING?? â Reverse proxy
38. 1014
1
5
stars, but growing steady
star it on http://github.com/primus/primus
1014
1 line of code to switch between
excluding adding a framework to your package.json
diïŹerent frameworks build-in
socket.io, engine.io, sockjs, websockets, browserchannel, but allows addition third party
5
1014
1
5
58. 1 line of code
module.exports = 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")!
});
66. message encoding
var primus = new Primus(server, { !
parser: "EJSON" // or binary-pack or a third party module!
});
67. message encoding
module.exports = {!
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")!
};
68. message encoding
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';!
});
transforming
72. broadcast
primus.write("message"); // 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");!
});
74. node.js
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 */});
76. plugins
// 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");
77. plugins
module.exports = {!
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")!
};
79. middleware
var server = require("http").createServer(fn)!
, primus = new Primus(server, { transformer:"sockjs" });!
!
primus.before(âsessionâ, require(âsession-parse-moduleâ))!
.before(âmiddleware-nameâ, "middleware-module-name");