Node.JS and WebSockets with Faye

7,189

Published on

Published in: Technology
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,189
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
81
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Transcript of "Node.JS and WebSockets with Faye"

  1. 1. Node.JS and WebSockets with Faye Matjaž Lipuš @MatjazL
  2. 2. Real-time web The real-time web is a set of technologies and practices which enable users to receive information as soon as it is published. http://en.wikipedia.org/wiki/Real-time_web
  3. 3. Real-time web - Server • Long, streaming connections • Apache, IIS • Node, nginx, Tornado, Thin, Netty
  4. 4. Real-time web - Client • Fast, small, simple API • Long polling, streaming, websockets • socket.IO, Faye
  5. 5. NodeJS • JavaScript bindings to system I/O • Server-side JavaScript (Google’s V8) • Most of Node is written in JavaScript • Current v0.2.4 more: http://nodejs.org
  6. 6. NodeJS - non blocking I/O • (almost) nothing blocks • Similar to EventMachine and Twisted  without "run()", simply enters event loop, like browser • No function should direct perform I/O • Stream everything; never force the buffering of data
  7. 7. NodeJS - non blocking I/O puts("Enter your name: "); var name = gets(); puts("Name: " + name); puts("Enter your name: "); gets(function (name) { puts("Name: " + name); });
  8. 8. NodeJS - One process and thread • Event loop  events  callbacks • Multi processes  web workers API  multi-node module
  9. 9. NodeJS - HTTP protocol • Built for web • Easy library and framework development http.createServer(function(req, res) { res.writeHead(200, { "Content-Type": "text/plain" }); res.end("Hello Worldn"); }).listen(8080);
  10. 10. NodeJS - JavaScript • Anonymous functions, closures • Only one callback at a time • I/O through DOM event callbacks • Javascript === evented programming
  11. 11. NodeJS - Modules • CommonJS • Built-in  process, util, buffer  fs, net, http • Third part  http://github.com/ry/node/wiki/modules  npm package manager
  12. 12. NodeJS - Addons • Glue to C and C++ libraries extern "C" void init (Handle<Object> target) • http://github.com/ry/node_postgres
  13. 13. NodeJS - In development • API changes • Unstable modules (SSL) • Short release cycles
  14. 14. Faye • Simple pub/sub messaging for the web • Bayeux more: http://faye.jcoglan.com
  15. 15. Faye - Concepts / patterns • Deferrable • Observable • Extensible • Timeouts • Logging • Set
  16. 16. Faye - Classes • Server • Connection • Client • Transport  HttpTransport  LocalTransport - in process  WebSocket  XHR - long polling  JSONP - callback polling • Channel • Subscription
  17. 17. Faye - Adapters • NodeAdapter • RackAdapter var server = new Faye.NodeAdapter({ mount: "/", timeout: 60 });
  18. 18. Faye - Server var Faye = require("faye"), server = new Faye.NodeAdapter(); server.listen(8000); server.getClient().publish("/email/new",{ text: "New email has arrived!", });
  19. 19. Faye - Client var client = new Faye.Client("http://localhost:8000/bayeux"); client.subscribe("/messages", function(message) { alert("Got a message: " + message.text); }); client.publish("/messages", { text: "Can anyone hear me?" });
  20. 20. Faye - Extensions var serverAuth = { incoming: function(message, callback) { if (message.channel === "/meta/subscribe") { checkAuth(message, function(authorized) { if (!authorized) { message.error = "Access denied"; } callback(message); }); } else { callback(message); } } }; server.addExtension(serverAuth); server.removeExtension(serverAuth);
  21. 21. Links • http://nodejs.org • http://faye.jcoglan.com • http://howtonode.org • http://s3.amazonaws.com/four.livejournal/20091117/ jsconf.pdf • http://nodejs.org/jsconf2010.pdf • http://howtonode.org/step-of-conductor
  22. 22. Contact Matjaž Lipuš @MatjazL
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×