why node.js?“Nodes goal is to provide an easy way to buildscalable network programs” http://nodejs.org/#about
what is node.js?• asynchronous i/o framework• core in c++ on top of v8• rest of it in javascript• swiss army knife for network related stuffs• can handle thousands of concurrent connections with minimal overhead (cpu/memory) on a single process
Single threadsynchronous I/0
Single threadsynchronous I/0
multiple threadsynchronous I/0
multiple threadsynchronous I/0
you can “always” scale with multiplemachines but it costs you $$$
but...what is HE doing?
CPU BOUND TASKS? but...what is HE doing?
CPU BOUND TASKS? ...OR I/o but... BOUNDwhat is HE TASKS? doing?
synchronous I/0function productsInCart(request, response) { var db = new Db() var user = new User(request) if (user.isAuthorized("cart/products")) { response.write( JSON.stringify( db.productsInCart(user.cartId()) ) ) } else { response.unauthorized() }}
synchronous I/0function productsInCart(request, response) { var db = new Db() var user = new User(request) if (user.isAuthorized("cart/products")) { response.write( JSON.stringify( db.productsInCart(user.cartId()) ) ) } else { response.unauthorized() }}
synchronous I/0function productsInCart(request, response) { var db = new Db() var user = new User(request) if (user.isAuthorized("cart/products")) { response.write( JSON.stringify( db.productsInCart(user.cartId()) ) ) } else { response.unauthorized() }}
synchronous I/0function productsInCart(request, response) { var db = new Db() var user = new User(request) if (user.isAuthorized("cart/products")) { response.write( JSON.stringify( db.productsInCart(user.cartId()) ) ) } else { response.unauthorized() }}
single threadasynchronous I/0
single threadasynchronous I/0 I am “callback” call me if you need me...
hello_world_server.js Event Emitter var http = require("http") var server = http.createServer(function(request, response) { response.writeHead(200, { "Content-Type": "plain/text" }) response.write("Hello Worldn") response.end() }) server.listen(8080) console.log("> SERVER STARTED")
hello_world_server.js Event Emitter var http = require("http") var server = http.createServer(function(request, response) { response.writeHead(200, { "Content-Type": "plain/text" }) response.write("Hello Worldn") response.end() }) server.listen(8080) console.log("> SERVER STARTED")
hello_world_server.js Event Emitter var http = require("http") var server = http.createServer(function(request, response) { response.writeHead(200, { "Content-Type": "plain/text" }) response.write("Hello Worldn") response.end() }) server.listen(8080) console.log("> SERVER STARTED")
hello_world_server.js Event Emitter var http = require("http") var server = http.createServer(function(request, response) { response.writeHead(200, { "Content-Type": "plain/text" }) response.write("Hello Worldn") response.end() }) server.listen(8080) console.log("> SERVER STARTED")
Event Emittercoder@apollo:~/Work/src/node/examples$ node hello_world_server.js> SERVER STARTED #1coder@apollo:~$ curl "http://localhost:8080/"Hello World #2
hello_world_server.js Event Emitter ... server.on("close", function() { console.log("> SERVER CLOSED") }) server.on("listening", function() { console.log("> SERVER STARTED") }) server.listen(8080)
Event Emittercoder@apollo:~/Work/src/node/examples$ node hello_world_server.js> SERVER STARTED #1coder@apollo:~$ curl "http://localhost:8080/"Hello World #2 #1> REQUEST STARTED> REQUEST CLOSED> SERVER CLOSED
why javascript?• Friendly callbacks• ubiquitous (well known)• no I/o primitives• one language to rule them all
web applications before: a webmind shift #1 server with some application logic application Web server application application application
web applications after: an applicationmind shift #1 accessible over http web server application
web applications after: an applicationmind shift #1 that can communicate and collaborate with the world web server application
web applications before: statefulmind shift #2 • no easy to scale • no easy to reuse v M c
web applications before: statefulmind shift #2 • no easy to scale • no easy to reuse v M c conversation application state state
web applications before: statefulmind shift #2 • no easy to scale • no easy to reuse v M c tightly coupled
web applications after: statelessmind shift #2 • easy to scale • easy to reuse v http M c
web applications after: statelessmind shift #2 • easy to scale • easy to reuse v http M cconversation application state state
web applications after: statelessmind shift #2 http web server M http statefull connection• easy to scale M• easy to reuse
no fluffjust stuff
What aboutcpu bound tasks?
the forklong_running_job.sh be with you #!/bin/bash for count in `seq 1 100`; do echo $count sleep 0.1 done
the forklong_running_server.js be with you var spawn = require("child_process").spawn, server = require("http").createServer() server.on("request", function(request, response) { var job = spawn("./long_running_job.sh") job.stdout.on("data", function(tick) { response.write(tick) }) job.on("exit", function() { response.end() }) })
the fork be with youcoder@apollo:~$ ab -c 1 -n 1 "http://localhost:8080/"...Concurrency Level: 1Time taken for tests: 10.531 seconds...coder@apollo:~$ ab -c 1 -n 2 "http://localhost:8080/"...Concurrency Level: 1Time taken for tests: 20.108 seconds...
the fork be with youcoder@apollo:~$ ab -c 2 -n 1 "http://localhost:8080/"...Concurrency Level: 2Time taken for tests: 10.634 seconds...coder@apollo:~$ ab -c 100 -n 100 "http://localhost:8080/"...Concurrency Level: 100Time taken for tests: 11.198 seconds...coder@apollo:~$ ab -c 500 -n 500 "http://localhost:8080/"...Concurrency Level: 500Time taken for tests: 31.082 seconds...
enter cometw at chwatch M spawn ch atw watch w w t a at ch ch
when to use it?• chat/messaging• real-time applications• intelligent proxies• high concurrency applications• communication hubs• coordinators
please tellme something bad about node.js
some warnings• release stable 0.4.7 (young)• lots of stuffs to look at• lots of half backed stuffs• retro compatibility???• bad at handling static contents• Bad at handling binary contents• hard to find organized and authoritative informations
vs
Questions?
gabriele lana gabriele.lana@cleancode.it twitter: @gabrielelanahttp://joind.in/3359