Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node, Real-time, HTML5: why wait?

Node, Real-time, HTML5: why wait?

Remy Sharp

May 03, 2012
Tweet

More Decks by Remy Sharp

Other Decks in Technology

Transcript

  1. The Plan • Context - why is real-time important? •

    What is Node.js? • Real-time client technology • My experience • Live coding live data...!
  2. Demos simple chat using long polling. Code is simple. function

    longPoll (data) { // :: snip :: handle the messages // start the long poll again $.ajax({ cache: false, url: "/recv", data: { since: CONFIG.last_message_time, id: CONFIG.id }, success: function (data) { longPoll(data); } });
  3. Apache fires up and kills php, and there is no

    persistence (out of the box).
  4. Traditional model • You greet receptionist at doctors • Who

    asks you to fill out forms and conversation back and forth • As you fill out, others are waiting and queuing behind you. • You're blocking.
  5. Event Driven • You greet receptionist at doctors • Who

    asks you to fill out forms and come back once they're complete • As you're filling them out, the next person is dealt with and so on. • Once you're done, you return the forms and are processed.
  6. function longPoll() { // <snip> $.ajax({ url: '/prices', type: 'json',

    success: function (data) { failures = 0; showPrices(data); longPoll(); }, error: longPollFailure }); }
  7. var es = new EventSource('/prices'); es.onmessage = function (event) {

    var data = JSON.parse(event.data); showPrices(data); };
  8. EventSource • Events emit from server • Read only stream

    • Simple API • Possible to polyfill (so IE support)
  9. EventSource • Supports connection dropping and resuming with Last-Event-ID •

    Supports bespoke events other than `message` • CORS is coming (x-domain SSE)
  10. var es = new EventSource('/prices'); es.onmessage = function (event) {

    var data = JSON.parse(event.data); showPrices(data); };
  11. var socket = new WebSocket('ws://somesite'); socket.onmessage = function (event) {

    var data = JSON.parse(event.data); showData(data); });
  12. var socket = new WebSocket('ws://somesite'); socket.onmessage = function (event) {

    var data = JSON.parse(event.data); showData(data); }); // <snip> data = { user: 'rem', data: payload }; socket.send(JSON.stringify(data));
  13. your mobile site add <script> iframe origin: jsconsole.com jsconsole server

    jsconsole client postMessage & onmessage EventSource Ajax post EventSource Ajax post
  14. Since jsconsole is a client side app, all I need

    to do is replace the static serving with Node and use connect
  15. Important part that's missing: res.end() and the response object is

    cached for use later on. app.get('/remote/:id/run', function (req, res) { var id = req.params.id; res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache' }); res.write('id:0\n\n'); sessions.run[id] = res; });
  16. app.post('/remote/:id/run', function (req, res) { var id = req.params.id, data

    = ''; // send to listening connections if (sessions.run[id]) { eventId++; data = 'data:' + req.body.data + '\nid:' + eventId); sessions.run[id].write(data + '\n\n'); } // close the ajax request res.writeHead(200, { 'Content-Type' : 'text/plain' }); res.end(); });
  17. var sse = new EventSource('/remote/' + id + '/run'), win

    = window.top; sse.onmessage = function (event) { win.postMessage(event.data, origin); };
  18. Remote Tilt • Create same message tunnel as jsconsole •

    Pipe orientation events from phone directly in to desktop browser • Orientation events now tested on desktop
  19. • Mobile users enter twitter handle, and hit my face

    • A socket establishes their connection and notifies server they're still active • Upon hit, message sent to main screen with their score (which is captured using EventSource) • Upon image capture on server, it's piped down to the connected devices.