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

Building Real-time Data Centric Apps with Node.js

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

Page 1: Building Real-time Data Centric Apps with Node.js

Building Real-time Data Centric Apps with Node.js

Jonathan Dinu @clearspandex

Page 2: Building Real-time Data Centric Apps with Node.js

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

Page 3: Building Real-time Data Centric Apps with Node.js

Evented? Say What!?!

Imagine a Food cart… your favorite food cart! On a nice little Wednesday.

Page 4: Building Real-time Data Centric Apps with Node.js

The Event loop

while (true) {

eventQueue.forEach.pop()

// execute code here

tick++;

}

Page 5: Building Real-time Data Centric Apps with Node.js

In Pictures

Event Queue

Process callback/event

FileSystem

Network

Process

Page 6: Building Real-time Data Centric Apps with Node.js

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)

Page 7: Building Real-time Data Centric Apps with Node.js

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));

} }

Page 8: Building Real-time Data Centric Apps with Node.js

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

Page 9: Building Real-time Data Centric Apps with Node.js

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.

Page 10: Building Real-time Data Centric Apps with Node.js

In Action!

# Ruby print(“Hello”) sleep(5) print(“world!”)

// node.js console.log(“Hello”); setTimeout(function() {

console.log(“world!”); }, 5000);

Page 11: Building Real-time Data Centric Apps with Node.js

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.

Page 12: Building Real-time Data Centric Apps with Node.js

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!

Page 13: Building Real-time Data Centric Apps with Node.js

Demo: http://bit.ly/SPsnO2

Code:

http://github.com/Jay-Oh-eN/TweetMap

Page 14: Building Real-time Data Centric Apps with Node.js

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

Page 15: Building Real-time Data Centric Apps with Node.js

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

Page 16: Building Real-time Data Centric Apps with Node.js

Thank You!

@clearspandex [email protected]

Please email me if you have any questions, feedback, or want to learn more!