Building Realtime Data-centric Apps with Node.js

Building Realtime Data-centric Apps with Node.js

The modern web development landscape is teeming with languages and frameworks, and new libraries are springing up every day. This class will focus on highlighting the areas in which Node.js excels to set itself apart, and reasons you might use (or not use) it to develop a web application. Using a live demo, students will walk through the code of a Node.js application and understand the key parts.


Jonathan Dinu

November 08, 2012


  1. Building Real-time Data Centric Apps with Node.js Jonathan Dinu @clearspandex

  2. What is Node.js !   Platform built on Chrome’s V8

    Javascript runtime. !   Written in C/C++ !   Evented, non-blocking I/O !   Perfect for: !   Data-intensive applications !   Real-time interactivity !   Distributed Architectures
  3. Evented? Say What!?! Imagine a Food cart… your favorite food

    cart! On a nice little Wednesday.
  4. The Event loop while (true) { eventQueue.forEach.pop() // execute code

    here tick++; }
  5. In Pictures Event Queue Process callback/event FileSystem Network Process

  6. Result: Very concurrent Processes !   Just like your operating

    system multiplexes limited resources !   Hard Disk I/O !   Network I/O !   CPU When resources become available, Node uses them. When unavailable, Node patiently schedules for them (Event-Loop)
  7. Concurrent != Parallel // Fibonacci sequence generator function f(n) {

    var start = 0; if(n == 0) return(s); if(n == 1) { s += 1; return(s); } else { return(f(n - 1) + f(n - 2)); } }
  8. Be Wary! !   Shared State Concurrency !   No

    threads == no separate context Apache Node.js PHP Thread PHP Thread PHP Thread Request Request Request Request Request Request
  9. Why Asynchrony :: Why Javascript !   Because that’s how

    I/O should be done! OS has done it for decades, why can’t web servers do it too. !   V8 has become one of the most advanced VMs out there !   Google supports it, and the Browser Wars mean it will only get better ! Javascript is ubiquitous, most know and if not it is “easy” to learn. !   First-class functions and closures fit the callback model.
  10. In Action! # Ruby print(“Hello”) sleep(5) print(“world!”) // node.js console.log(“Hello”);

    setTimeout(function() { console.log(“world!”); }, 5000);
  11. Landscape Framework/Language Stengths Weaknesses Server Side MVC (Rails/ Django) Full

    stack frameworks (all encompassing). Rapidly prototype complex apps or build from the ground up. Monolithic and very heavy. Hard to swap out components to use a different library (walled garden) Node.js (Express/ Connect) The “pipes”. Great for connecting components and apps with multiple interacting services Often too low level for complex workflows and heavy CRUD applications. Client Side MVC (Backbone, Angular, Ember) Very responsive and interactive. Manages complex transition and state on the client. Often coupled with a server framework. Only lives on the client so unless you persist state with Ajax or browser storage, changes and updates are all forgotten on page navigation.
  12. Putting it all together! !   Node is the “pipes”

    – really shines when you have to connect multiple components => Service oriented architecture !   The asynchronous nature makes it very efficient at handling multiple independent processes. !   But the single thread of execution means it is bad at computing in parallel in its context. Offshore it!
  13. Demo: Code: TweetMap

  14. Tweet Map Twitter API Redis Express App (app.js) +

    Tweet Server (tweet.js) Browser HTTP Request Requests Responses Messages Twitter API Request Twitter API Response (Tweet JSON) Tweet aggregates PubSub alert HTTP Response count update
  15. Resources •  Npm (node package manager) – •  node_redis

    - •  twit - •  Express Web Framework – • – •  D3.js – •  Redis –
  16. Thank You! @clearspandex Please email me if you have

    any questions, feedback, or want to learn more!