Transforming WebSockets
- 10. that nobody told youthat nobody told youthat nobody told you
The partsThe partsThe parts
- 12. user agent sniff
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");!
};
- 18. Don’t use self signed SSL
certificates, not for development and
not in production
- 19. 4G, 3G, LTE, mobile providers WTF
ARE YOU DOING?? — Reverse proxy
- 31. They all have different use cases and
API's.
!
Changing product specs == rewrite
- 32. we could change this toowe could change this toowe could change this too
What if…What if…What if…
- 34. Primus wraps and improves popular
real-time frameworks. So you can
focus on building apps.
- 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
different frameworks build-in
socket.io, engine.io, sockjs, websockets, browserchannel, but allows addition third party
5
1014
1
5
- 41. 50
31
50
31
50 different plugins
substream, emit, primus-emitter, primus-cluster, primus-rooms, primus-multiplex, primus-redis
different authors
damonoehlman, mmalecki, jcrugzz, daffl, balupton, cayasso & many more
31
- 43. why was primus inventedwhy was primus inventedwhy was primus invented
HistoryHistoryHistory
- 53. learning primus
'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);
- 56. 1 line of code
var primus = new Primus(server, { !
transformer: “sockjs" // engine.io, socket.io etc!
});
- 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")!
});
- 63. stream compatible
primus.on("end", function disconnected() {!
console.log("connection ended");!
});!
!
primus.end();!
primus.write();!
!
fs.createReadStream(__dirname + '/index.html').pipe(spark, {!
end: false!
});
- 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");