Upload
jonathan-dinu
View
696
Download
4
Embed Size (px)
DESCRIPTION
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.
Citation preview
Building Real-time Data Centric Apps with Node.js
Jonathan Dinu @clearspandex
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
Evented? Say What!?!
Imagine a Food cart… your favorite food cart! On a nice little Wednesday.
The Event loop
while (true) {
eventQueue.forEach.pop()
// execute code here
tick++;
}
In Pictures
Event Queue
Process callback/event
FileSystem
Network
Process
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)
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));
} }
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
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.
In Action!
# Ruby print(“Hello”) sleep(5) print(“world!”)
// node.js console.log(“Hello”); setTimeout(function() {
console.log(“world!”); }, 5000);
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.
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!
Demo: http://bit.ly/SPsnO2
Code:
http://github.com/Jay-Oh-eN/TweetMap
Tweet Map
Twitter API
Redis
Express App (app.js)
+ Socket.io
Tweet Server
(tweet.js)
Browser
HTTP Request
Requests Responses Messages
Twitter API Request
Twitter API Response (Tweet JSON)
Tweet aggregates PubSub alert
HTTP Response
Socket.io count update
Resources
• Npm (node package manager) – npmjs.org • node_redis - https://github.com/mranney/node_redis
• twit - https://github.com/ttezel/twit
• Express Web Framework – expressjs.com
• Socket.io – socket.io
• D3.js – d3js.org
• Redis – redis.io
Thank You!
@clearspandex [email protected]
Please email me if you have any questions, feedback, or want to learn more!