Nodejs - A quick tour (v6)

68
node.js A quick tour 20.04.2011 (v6) Felix Geisendörfer

description

Internal presentation at Gameforge (Karlsruhe).

Transcript of Nodejs - A quick tour (v6)

Page 1: Nodejs - A quick tour (v6)

node.jsA quick tour

20.04.2011 (v6)

Felix Geisendörfer

Page 2: Nodejs - A quick tour (v6)

@felixge

Twitter / GitHub / IRC

Page 3: Nodejs - A quick tour (v6)

About

• Felix Geisendörfer

• Berlin, Germany

• Debuggable Ltd

Page 4: Nodejs - A quick tour (v6)

Core Contributor

&

Module Author

node-mysql node-formidable

Page 5: Nodejs - A quick tour (v6)

File uploading & processing as an infrastructure service for web & mobile applications.

Page 6: Nodejs - A quick tour (v6)

Audience?

Page 7: Nodejs - A quick tour (v6)

Server-side JavaScript

Page 8: Nodejs - A quick tour (v6)

Server-side JavaScript

• Netscape LiveWire (1996)

• Rhino (1997)

• 50+ plattforms since then

Page 9: Nodejs - A quick tour (v6)

What was the problem?

• Slow engines

• Lack of interest in JS until ~2005

• Better competing plattforms

Page 10: Nodejs - A quick tour (v6)

Why JavaScript?3 Reasons

Page 11: Nodejs - A quick tour (v6)

#1 - The good parts

Page 12: Nodejs - A quick tour (v6)

#2 - JS Engine Wars

V8 (Chrome)

Chakra (IE9)

SpiderMonkey (Firefox) JavaScriptCore (Safari)

Carakan (Opera)

check out: arewefastyet.com

Page 13: Nodejs - A quick tour (v6)

#3 - No I/O in the core

Page 14: Nodejs - A quick tour (v6)

Node's goal is to provide an easy way to build scalable network programs.

Page 15: Nodejs - A quick tour (v6)

Node.js

• Created by Ryan Dahl

• Google’s V8 JavaScript engine (no DOM)

• Module system, I/O bindings, Common Protocols

Page 16: Nodejs - A quick tour (v6)

Installing

$ git clone \git://github.com/ry/node.git$ cd node$ ./configure$ make install

Page 17: Nodejs - A quick tour (v6)

Hello World

$ cat test.jsconsole.log('Hello World');

$ node test.jsHello World

Page 18: Nodejs - A quick tour (v6)

Ingredients

V8

libev

http_parser

c-ares

libeio

Page 19: Nodejs - A quick tour (v6)

Philosophy

• Just enough core-library to do I/O

• Non-blocking

• Close to the underlaying system calls

Page 20: Nodejs - A quick tour (v6)

Non-blocking I/O

Page 21: Nodejs - A quick tour (v6)

Blocking I/O

var a = db.query('SELECT A');console.log('result a:', a);

var b = db.query('SELECT B');console.log('result b:', b);

Time = SUM(A, B)

Page 22: Nodejs - A quick tour (v6)

Non-Blocking I/O

db.query('SELECT A', function(result) { console.log('result a:', result);});

db.query('SELECT B', function(result) { console.log('result b:', result);});

Time = MAX(A, B)

Page 23: Nodejs - A quick tour (v6)

Non-Blocking I/O

• Offload most things to the kernel and poll for changes (select, epoll, kqueue, etc.)

• Thread pool for anything else

Page 24: Nodejs - A quick tour (v6)

Single Threadedvar a = [];function first() { a.push(1); a.push(2);}

function second() { a.push(3); a.push(4);}

setTimeout(first, 10);setTimeout(second, 10);

Page 25: Nodejs - A quick tour (v6)

Single Threaded

• [1, 2, 3, 4] ?

• [3, 4, 1, 2] ?

• [1, 3, 2, 4] ?

• BAD_ACCESS ?

var a = [];function first() { a.push(1); a.push(2);}

function second() { a.push(3); a.push(4);}

setTimeout(first, 10);setTimeout(second, 10);

Page 26: Nodejs - A quick tour (v6)

Single Threaded

• [1, 2, 3, 4]

• [3, 4, 1, 2]

• [1, 3, 2, 4]

• BAD_ACCESS

var a = [];function first() { a.push(1); a.push(2);}

function second() { a.push(3); a.push(4);}

setTimeout(first, 10);setTimeout(second, 10);

Page 27: Nodejs - A quick tour (v6)

API Overview

Page 28: Nodejs - A quick tour (v6)

CommonJS Modules$ cat hello.jsexports.world = function() { return 'Hello World';};

$ cat main.jsvar hello = require('./hello');console.log(hello.world());

$ node main.jsHello World

Page 29: Nodejs - A quick tour (v6)

Child processes$ cat child.jsvar spawn = require('child_process').spawn;var cmd = 'echo hello; sleep 1; echo world;';

var child = spawn('sh', ['-c', cmd]);child.stdout.on('data', function(chunk) { console.log(chunk.toString());});

$ node child.js"hello\n"# 1 sec delay"world\n\n"

Page 30: Nodejs - A quick tour (v6)

Http Server$ cat http.jsvar http = require('http');http.createServer(function(req, res) { setTimeout(function() { res.writeHead(200); res.end('Thanks for waiting!'); }, 1000);}).listen(4000);

$ curl localhost:4000# 1 sec delayThanks for waiting!

Page 31: Nodejs - A quick tour (v6)

Tcp Server$ cat tcp.jsvar tcp = require('tcp');tcp.createServer(function(socket) { socket .on('connect', function() { socket.write("Hi, How Are You?\n> "); }) .on('data', function(data) { socket.write(data); });}).listen(4000);

$ nc localhost 4000Hi, How Are You?> Great!Great!

Page 32: Nodejs - A quick tour (v6)

DNS

$ cat dns.jsvar dns = require('dns');dns.resolve('nodejs.org', function(err, addresses) { console.log(addresses);});

$ node dns.js[ '8.12.44.238' ]

Page 33: Nodejs - A quick tour (v6)

Watch File$ cat watch.jsvar fs = require('fs');fs.watchFile(__filename, function() { console.log('You changed me!'); process.exit(0);});

$ node watch.js# edit watch.jsYou changed me!

Page 34: Nodejs - A quick tour (v6)

And much more

• UDP

• Crypto

• Assert

• Buffer

• VM

• EcmaScript5

...

Page 35: Nodejs - A quick tour (v6)

Suitable Applications

Page 36: Nodejs - A quick tour (v6)

Suitable Applications

• Singe-page apps

• Real time

• Crawlers

Page 37: Nodejs - A quick tour (v6)

More Applications

• Async chaining of unix tools

• Streaming

• File uploading

Page 38: Nodejs - A quick tour (v6)

Interesting projects

Page 39: Nodejs - A quick tour (v6)

Package management

Page 40: Nodejs - A quick tour (v6)

Web Frameworks

• Express.js (Sinatra clone)

• Fab.js (Mind-bending & awesome)

Page 41: Nodejs - A quick tour (v6)

Socket.IOvar http = require('http');var io = require('socket.io');

var server = http.createServer();server.listen(80);

var socket = io.listen(server);socket.on('connection', function(client){ client.send('hi');

client.on('message', function(){ … }) client.on('disconnect', function(){ … })});

Server

Page 42: Nodejs - A quick tour (v6)

Socket.IO

<script src="http://{node_server_url}/socket.io/socket.io.js" /><script> var socket = new io.Socket({node_server_url}); socket.connect(); socket.on('connect', function(){ … }) socket.on('message', function(){ … }) socket.on('disconnect', function(){ … })</script>

Client

Page 43: Nodejs - A quick tour (v6)

Socket.IO

• WebSocket

• Adobe® Flash® Socket

• AJAX long polling

• AJAX multipart streaming

• Forever Iframe

• JSONP Polling

Chooses most capable transport at runtime!

Page 44: Nodejs - A quick tour (v6)

1600+ modules in npm

Page 45: Nodejs - A quick tour (v6)

Ready for production?

Page 46: Nodejs - A quick tour (v6)

Ready for production?

• Handled over 100.000 file uploads

• Several TB of data

• No bugs, Memory usage betwen 30 - 80 MB

Our experience at Transloadit.com

Page 47: Nodejs - A quick tour (v6)

Ready for production?

• 0.4.6 was released on April 13th

• 0.4.x API is stable

• Very few bugs, but YMMV

Page 48: Nodejs - A quick tour (v6)

Things that suck

Page 49: Nodejs - A quick tour (v6)

Things that suck• Stack traces are lost at the event loop

boundary

• Utilizing multiple cores requires multiple processes

• V8: 1.9 GB heap limit / GC can be problematic

Page 50: Nodejs - A quick tour (v6)

Community

Page 52: Nodejs - A quick tour (v6)

Community

• Mailing list (nodejs, nodejs-dev)

• IRC (#node.js)

Page 53: Nodejs - A quick tour (v6)

Hosting

Page 54: Nodejs - A quick tour (v6)

More information

nodeguide.com

Page 55: Nodejs - A quick tour (v6)

Meet the community

nodecamp.de

June 11 - 12

Cologne, Germany

Tickets available 12pm CET tomorrow

Page 57: Nodejs - A quick tour (v6)

Bonus Slides

Page 58: Nodejs - A quick tour (v6)

Speed

Page 59: Nodejs - A quick tour (v6)

Speed

var http = require(’http’)var b = new Buffer(1024*1024);

http.createServer(function (req, res) { res.writeHead(200); res.end(b);}).listen(8000);

by Ryan Dahl

Page 60: Nodejs - A quick tour (v6)

Speed

100 concurrent clients1 megabyte response

node 822 req/secnginx 708thin 85mongrel 4

(bigger is better)by Ryan Dahl

Page 61: Nodejs - A quick tour (v6)

Speed

• NGINX peaked at 4mb of memory

• Node peaked at 60mb

• Very special circumstances

by Ryan Dahl

Page 62: Nodejs - A quick tour (v6)

What about all those callbacks?

Page 63: Nodejs - A quick tour (v6)

db.query('SELECT A', function() { db.query('SELECT B', function() { db.query('SELECT C', function() { db.query('SELECT D', function() { // ouch }); }); });});

Page 64: Nodejs - A quick tour (v6)
Page 65: Nodejs - A quick tour (v6)

You are holding it wrong!

Page 66: Nodejs - A quick tour (v6)

Nested Callbacks

• Function does too many things, break it up

• Use an sequential abstraction library

• Consider using a JS language extension

Page 67: Nodejs - A quick tour (v6)

Nested Callbacks

err1, result1 = db.query! 'SELECT A'err2, result2 = db.query! 'SELECT B'err3, result3 = db.query! 'SELECT C'err4, result4 = db.query! 'SELECT D'

Kaffeeine