node.js: a guided tour

57
Node.js A Guided Tour Presenter: C. Aaron Cois, Ph.D.

Upload: cacois

Post on 28-Oct-2014

97 views

Category:

Documents


0 download

DESCRIPTION

This is the slide deck from a talk entitled "Node.js: A Guided Tour" given at Pittsburgh TechFest, 2012. This talk was intended as an introduction to the Node.js framework for fast, scalable network and web applications.Want to learn how to program with Node.js? Check out:http://www.udemy.com/learn-nodejs-by-example/More information on Node.js can be found at the presenter's website: http://www.codehenge.net

TRANSCRIPT

Page 1: Node.js: A Guided Tour

Node.jsA Guided Tour

Presenter: C. Aaron Cois, Ph.D.

Page 2: Node.js: A Guided Tour

The web is changing It used to be about consumption

Page 3: Node.js: A Guided Tour

Real-time Interaction

Page 4: Node.js: A Guided Tour

Real-time Interaction Real-Time Games Chat Stock tickers Twitter Feeds Collaboration Creation

…on a massive scale

Page 5: Node.js: A Guided Tour

What do we need to make it happen? Fast, persistent I/O

HTTP wasn’t built for this Server’s need to push data to clients Polling is slow and inefficient

Scalability Usability Maintainability

Page 6: Node.js: A Guided Tour
Page 7: Node.js: A Guided Tour

Outline What is Node.js? Technology Overview How does it work? Demo Code! Deployment and Hosting

Page 8: Node.js: A Guided Tour

What is Node.js?

Page 9: Node.js: A Guided Tour

Node.js Node.js is an event-driven, server-

side JavaScript environment Based on the V8 JavaScript Engine,

developed by Google Most importantly, node.js is a

server-side runtime environment, that compiles and executes JavaScript very

efficiently.

Page 10: Node.js: A Guided Tour

Platforms Runs on OSX, Linux, Windows

Clickable installers for: Windows Mac OSX

Linux has apt-get and yum

Page 11: Node.js: A Guided Tour

Why Node? Node.js is specifically designed for

building fast, efficient, scalable network applications

Node uses an event-driven, non-blocking I/O model to maximize efficiency

Page 12: Node.js: A Guided Tour

Technology: V8 Engine Developed by Google Ships with the Google Chrome web browser Allows Chrome to run JavaScript code much

faster It does this by compiling the JavaScript directly

into native machine code As opposed to interpreting JavaScript, or

execute it as bytecode What does this mean for us, and for Node.js?

Page 13: Node.js: A Guided Tour
Page 14: Node.js: A Guided Tour

Technology: JavaScript JavaScript is:

A fully-functional programming language Capable of doing anything other traditional

languages (C++, Java, Ruby, etc) can do Has an excellent event model Traditionally resigned to the context of the

web application frontend i.e. running inside a web browser

Page 15: Node.js: A Guided Tour

Technology: JavaScript2

There’s no reason the JavaScript language can’t be used elsewhere (say, server-side) This is where node.js comes in, executing

JavaScript efficiently on the server-side JavaScript brings to Node.js:

Natural event-based programming, ideal for client-server applications

A known language, with low overhead

Page 16: Node.js: A Guided Tour

Who is using Node.js?

Page 17: Node.js: A Guided Tour

How does it work?

Page 18: Node.js: A Guided Tour

The Basic Idea

I/O is expensive

Page 19: Node.js: A Guided Tour

*http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/

Page 20: Node.js: A Guided Tour

Ways to deal with I/O Synchronous

One requests at a time, first come, first serve

Fork New process for each request

Threads New thread for each request

*http://www.nightmare.com/medusa/async_sockets.html

Page 21: Node.js: A Guided Tour

Another Thesis

Thread-per-connection is memory-expensive

Page 22: Node.js: A Guided Tour
Page 23: Node.js: A Guided Tour

Traditional Threaded ModelN worker threads/processesEach incoming connection handed to a

worker That worker is now “in use”, and can

handle no other connection, even if it is waiting on: File I/O DB I/O Network I/O etc

Page 24: Node.js: A Guided Tour

Waiting on File I/O…

Waiting on Network Response…

The life of a worker…

SQL

Que

ry

Waiting on DB…

Read

File

Send

HTT

P Re

ques

t

Time

Page 25: Node.js: A Guided Tour

Waiting on File I/O…

Waiting on Network Response…

The life of a worker…

SQL

Que

ry

Waiting on DB…

Read

File

Send

HTT

P Re

ques

t

Time

Blocking Wastes Cycles

Page 26: Node.js: A Guided Tour

The Other Basic Idea

Writing (Good) Threaded Code is DIFFICULT

Page 27: Node.js: A Guided Tour

The life of N workers…

Time

Thread 1

Thread 3

Thread 2

Thread 4

Page 28: Node.js: A Guided Tour

The life of N workers…

Time

Thread 1

Thread 3

Thread 2

Thread 4

Page 29: Node.js: A Guided Tour

The life of N workers…

Time

Thread 1

Thread 3

Thread 2

Thread 4

ALL PROCESSES IDLE

Page 30: Node.js: A Guided Tour

Even worse… If all threads are in use, every incoming

connection is blocked

This can cause massive traffic jams on high-throughput applications

Page 31: Node.js: A Guided Tour

Is this the only way?

There is another…

Page 32: Node.js: A Guided Tour

The Node.js way Axiom:

Multi-Threaded code Is difficult to write Is difficult to debug Sucks up more dev/test/maintenance cycles Most often has inefficient performance

Conclusion: Screw it: Write code using a single thread

Page 33: Node.js: A Guided Tour

Single threaded?!?Skeptical? I don’t blame you

But hear me out…

Page 34: Node.js: A Guided Tour

Node.js Event Loop Event Loop (from Wikipedia):

A “construct that waits for and dispatches events or messages in a program”

Instead of performing I/O ourselves, we dispatch I/O events to Node’s event loop It handles threads, process optimization,

concurrency, etc

Page 35: Node.js: A Guided Tour

Node.js Event Loop

File I/O command to event loop

Net I/O command to event loop

DB I/O command to event loop

Read

File

SQL

Que

ry

Send

HTT

P Re

ques

t

Net

I/O

Cal

lbac

k

File

I/O

Cal

lbac

k

DB

I/O C

allb

ack

Time

Page 36: Node.js: A Guided Tour

Node.js Event Loop

File I/O command to event loop

Net I/O command to event loop

DB I/O command to event loop

Open for more work!

Read

File

SQL

Que

ry

Send

HTT

P Re

ques

t

Net

I/O

Cal

lbac

k

File

I/O

Cal

lbac

k

DB

I/O C

allb

ack

Time

Page 37: Node.js: A Guided Tour

Node.js app code… Is run entirely in a single thread Passes I/O requests to the event loop,

along with callbacks

Your code then: Goes to sleep Uses no system resources Will be notified via callback when I/O is

complete

Page 38: Node.js: A Guided Tour

Callback example

var filename = “test_file.txt”;

fs.open(filename, “w”, function(err, file) { if (err) throw err;});

Page 39: Node.js: A Guided Tour

Callback example

var file = (“test_file.txt”);

fs.open(file, “w”, function(err, file) { if (err) throw err;});

Filesystem module forwards task to event loop

Page 40: Node.js: A Guided Tour

Callback example

var file = (“test_file.txt”);

fs.open(file, “w”, function(err, file) { if (err) throw err;});

Callback is invoked when work is complete

Page 41: Node.js: A Guided Tour

This is not magic The following:

Will block the entire Node event loop for 5 seconds

for(i=0; i<5; i++) { sleep(1000);}

Page 42: Node.js: A Guided Tour

Node is in charge Let Node.js handle

Dispatch Concurrency (most) Async operations

What Node doesn’t promise: To not block when you tell it to Order of execution (e.g. forked parallel

processes)

Page 43: Node.js: A Guided Tour

Interlude: Modules

Page 44: Node.js: A Guided Tour

Node.js API Node provides an API in the form of

modules (a.k.a. libraries) Modules work with event loop to dispatch

async tasks

API modules are installed along with Node They provide standard application framework

functionality STDIO: console logging, timing, tracing File System: File system access …etc

Page 45: Node.js: A Guided Tour

A Few Good Modules Net Network socket support

HTTP HTTP communication

File System File system access

Crypto Cryptography

Streams STDIO

Many more…

Page 46: Node.js: A Guided Tour

Node.js Community Additionally, the Node.js community

maintains excellent modules to enhance the capabilities of Node

These modules operate as Libraries Toolkits Frameworks …and much more.

Page 47: Node.js: A Guided Tour

Notable Community Modules

Express Web App Framework

Socket.io Websockets, etc

Passport/Everyauth Authentication

Jade HTML Template Engine

Connect Middleware collection

Less Simplified CSS

Page 48: Node.js: A Guided Tour

Let’s see some code!

Page 49: Node.js: A Guided Tour

Node Web Server

var http = require('http');

http.createServer(function (req, res) {

res.writeHead(200, {'Content-Type': 'text/plain'});

res.end('Hello World\n');

}).listen(1337, '127.0.0.1');

Page 50: Node.js: A Guided Tour

Node TCP Listener

var net = require('net');

var server = net.createServer(function (socket) {

socket.write('Echo server\r\n');

socket.pipe(socket);

});

server.listen(1337, '127.0.0.1');

Page 51: Node.js: A Guided Tour

Live Demo

http://techfestchat.jit.su

Hosting courtesy of Nodejitsu

Page 52: Node.js: A Guided Tour

Any .NET Devs in the room?

Microsoft has been expending a lot of effort to make Node a first class framework

And now…

Page 53: Node.js: A Guided Tour

A sneak preview!

Page 54: Node.js: A Guided Tour

MS WebMatrix 2

Open WebMatrix 2

Page 55: Node.js: A Guided Tour

Node.js Deployment and Hosting

Page 56: Node.js: A Guided Tour

Cloud Hosting Heroku Microsoft Azure Nodejitsu Cloud Foundry Nodester DotCloud Appfog Joyent (coming soon) …

Page 57: Node.js: A Guided Tour

Thanks! Feel free to look me up at:

http://www.codehenge.net

I love questions, collaborations, and talking with people!

If you are interested in formal Node.js learning, I also have a course available at:

http://www.udemy.com/learn-nodejs-by-example/