Node.js - A Quick Tour

37
node.js A quick tour by Felix Geisendörfer

description

Slides for my talk at the Berlin JavaScript user group on Jan 12th, 2010.

Transcript of Node.js - A Quick Tour

Page 1: Node.js - A Quick Tour

node.jsA quick tour

by Felix Geisendörfer

Page 2: Node.js - A Quick Tour

Introduction

Page 3: Node.js - A Quick Tour

Why?

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

-- nodejs.org

Page 4: Node.js - A Quick Tour

How?

Keep slow operations from blocking other operations.

Page 5: Node.js - A Quick Tour

Traditional I/O

var data = file.read('file.txt');doSomethingWith(data);

Something is not right here

Page 6: Node.js - A Quick Tour

Traditional I/O

var data = file.read('file.txt');

// zzzZZzzz

doSomethingWith(data);

Don’t waste those cycles!

FAIL!

Page 7: Node.js - A Quick Tour

Async I/O

file.read('file.txt', function(data) { doSomethingWith(data);});

doSomethingElse();

WIN !

No need to wait for the disk, do something else meanwhile!

Page 8: Node.js - A Quick Tour

The Present

Page 9: Node.js - A Quick Tour

CommonJS Modules

exports.world = function() { return 'Hello World';};

hello.js

main.js

var hello = require('./hello');var sys = require('sys');sys.puts(hello.world());

$ node main.jsHello World

Page 10: Node.js - A Quick Tour

Child processes

var child = process.createChildProcess('sh',['-c', 'echo hello; sleep 1; echo world;']);child.addListener('output', function (chunk) { p(chunk);});

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

child.js

Page 11: Node.js - A Quick Tour

Http Server

var http = require('http');http.createServer(function(req, res) { setTimeout(function() { res.sendHeader(200, {'Content-Type': 'text/plain'}); res.sendBody('Thanks for waiting!'); res.finish(); }, 1000);}).listen(4000);

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

Page 12: Node.js - A Quick Tour

Tcp Servervar tcp = require('tcp');tcp.createServer(function(socket) { socket.addListener('connect', function() { socket.send("Hi, How Are You?\n> "); }); socket.addListener('receive', function(data) { socket.send(data); });}).listen(4000);

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

Page 13: Node.js - A Quick Tour

DNS

var dns = require('dns');dns.resolve4('nodejs.org') .addCallback(function(r) { p(r); });

dns.js

$ node dns.js[ "97.107.132.72"]

Page 14: Node.js - A Quick Tour

Watch File

process.watchFile(__filename, function() { puts('You changed me!'); process.exit();});

watch.js

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

Page 15: Node.js - A Quick Tour

ECMAScript 5

• Getters / settersvar a = {};a.__defineGetter__('foo', function() { return 'bar';});puts(a.foo);

• Array: filter, forEach, reduce, etc.

• JSON.stringify(), JSON.parse()

Page 16: Node.js - A Quick Tour

There is only 1 thread

file.read('file.txt', function(data) { // Will never fire});

while (true) { // this blocks the entire process}

Good for conceptual simplicityBad for CPU-bound algorithms

Page 17: Node.js - A Quick Tour

The Future

Page 18: Node.js - A Quick Tour

Web workers

• Multiple node processes that do interprocess communication

• CPU-bound algorithms can run separately

• Multiple CPU cores can be used efficiently

Page 19: Node.js - A Quick Tour

Move C/C++ stuff to JS

• Simplifies the code base

• Makes contributions easier

• Low-level bindings = more flexibility

Page 20: Node.js - A Quick Tour

Better Socket Support

• Support for unix sockets, socketpair(), pipe()

• Pass sockets between processes " load balance requests between web workers

• Unified socket streaming interfaces

Page 21: Node.js - A Quick Tour

Even more ...

• Connecting streams

• Http parser bindings

• No memcpy() for http requests

var f = file.writeStream('/tmp/x.txt');connect(req.body, f);

+ hot code reloading!

Page 22: Node.js - A Quick Tour

Suitable Applications

• Web frameworks

• Real time

• Crawlers

Page 23: Node.js - A Quick Tour

More Applications

• Process monitoring

• File uploading

• Streaming

Page 24: Node.js - A Quick Tour

Demo Time!

Page 25: Node.js - A Quick Tour

Http Chat in 14 LoCvar http = require('http'), messages = [];

http.createServer(function(req, res) { res.sendHeader(200, {'Content-Type' : 'text/plain'}); if (req.url == '/') { res.sendBody(messages.join("\n")); } else if (req.url !== '/favicon.ico') { messages.push(decodeURIComponent(req.url.substr(1))); res.sendBody('ok!'); } res.finish();}).listen(4000);

Page 26: Node.js - A Quick Tour

http://debuggable.com:4000/

http://debuggable.com:4000/<msg>

The chat room:

Send a message:

Page 27: Node.js - A Quick Tour

Questions?

☝@felixge

http://debuggable.com/$✎

Page 28: Node.js - A Quick Tour

Bonus Slides!

Page 29: Node.js - A Quick Tour

Dirty

NoSQL for the little man!

Page 30: Node.js - A Quick Tour

Dirty

Dirty

JavaScript ViewsDisk Persistence

Memory StoreSpeed > Safety

Page 31: Node.js - A Quick Tour

Dirty Hello World

$ node hello.js$ cat test.dirty {"hello":"dirty world!","_key":"3b8f86..."}{"looks":"nice","_key":"my-key"}

var Dirty = require('../lib/dirty').Dirty, posts = new Dirty('test.dirty');

posts.add({hello: 'dirty world!'});posts.set('my-key', {looks: 'nice'});

hello.js

Page 32: Node.js - A Quick Tour

Reloading from Disk

var Dirty = require('../lib/dirty').Dirty, posts = new Dirty('test.dirty');

posts.load() .addCallback(function() { p(posts.get('my-key')); });

$ node hello.js{"looks": "nice", "_key": "my-key"}

hello.js

Page 33: Node.js - A Quick Tour

Filtering recordsvar Dirty = require('../lib/dirty').Dirty, posts = new Dirty('test.dirty');

posts.load() .addCallback(function() { var docs = posts.filter(function(doc) { return ('hello' in doc); }); p(docs); });

$ node hello.js[{"hello": "dirty world!", "_key": "3b8f86..."}]

hello.js

Page 34: Node.js - A Quick Tour

Benchmarks

Do your own!

Page 35: Node.js - A Quick Tour

My Results

• Set: 100k docs / sec

• Iterate: 33 million docs / sec

• Filter: 14 million docs / sec

(on my laptop - your milage may vary)

Page 36: Node.js - A Quick Tour

Use Cases

• Small projects (db < memory)

• Rapid prototyping

• Add HTTP/TCP interface and scale

Page 37: Node.js - A Quick Tour

http://github.com/felixge/node-dirty

(or google for “dirty felixge”)