nodejs explained with examples
DESCRIPTION
Nodejs Explained with ExamplesTRANSCRIPT
![Page 1: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/1.jpg)
this presentation code is onhttps://github.com/gabrielelana/node-examples
![Page 2: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/2.jpg)
gabriele [email protected]
twitter: @gabrielelana
this presentation code is onhttps://github.com/gabrielelana/node-examples
![Page 3: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/3.jpg)
“Node's goal is to provide an easy
way to build scalable network
programs”
http://nodejs.org/#about
why node.js?
![Page 4: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/4.jpg)
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
![Page 5: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/5.jpg)
Single threadsynchronous I/0
![Page 6: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/6.jpg)
Single threadsynchronous I/0
![Page 7: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/7.jpg)
multiple threadsynchronous I/0
![Page 8: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/8.jpg)
multiple threadsynchronous I/0
![Page 9: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/9.jpg)
you can“always”
scale withmultiple
machines butit costsyou $$$
![Page 10: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/10.jpg)
![Page 11: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/11.jpg)
but...what is HEdoing?
![Page 12: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/12.jpg)
but...what is HEdoing?
CPU BOUNDTASKS?
![Page 13: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/13.jpg)
but...what is HEdoing?
CPU BOUNDTASKS?
...OR I/oBOUNDTASKS?
![Page 14: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/14.jpg)
functionproductsInCart(request,response){vardb=newDb()varuser=newUser(request)if(user.isAuthorized("cart/products")){response.write(JSON.stringify(db.productsInCart(user.cartId())))}else{response.unauthorized()}}
synchronous I/0
![Page 15: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/15.jpg)
functionproductsInCart(request,response){vardb=newDb()varuser=newUser(request)if(user.isAuthorized("cart/products")){response.write(JSON.stringify(db.productsInCart(user.cartId())))}else{response.unauthorized()}}
synchronous I/0
![Page 16: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/16.jpg)
functionproductsInCart(request,response){vardb=newDb()varuser=newUser(request)if(user.isAuthorized("cart/products")){response.write(JSON.stringify(db.productsInCart(user.cartId())))}else{response.unauthorized()}}
synchronous I/0
![Page 17: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/17.jpg)
functionproductsInCart(request,response){vardb=newDb()varuser=newUser(request)if(user.isAuthorized("cart/products")){response.write(JSON.stringify(db.productsInCart(user.cartId())))}else{response.unauthorized()}}
synchronous I/0
![Page 18: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/18.jpg)
single threadasynchronous I/0
![Page 19: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/19.jpg)
http://www.infoq.com/presentations/Death-by-Accidental-Complexity
single source eventsSTAT
ES
EVENTS
![Page 20: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/20.jpg)
single source eventsfunctionproductsInCart(request,response){vardb=null,user=null,...createDb()handle(function(source,event){if(event["name"]==="createDb"){if(db===null){db=event.datacreateUser(request)}else{????}}elseif(event["name"]==="createUser"){if(user===null){user=event.data...}else{???}...}else{source.push(event,state)}},"_initial")}
![Page 21: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/21.jpg)
single threadasynchronous I/0
single threadasynchronous I/0
I am“callback”
call meif you
need me...
![Page 22: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/22.jpg)
single threadasynchronous I/0
single threadasynchronous I/0
![Page 23: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/23.jpg)
multiple source events(local state)
STAT
ES
EVENTS
![Page 24: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/24.jpg)
functionproductsInCart(request,response){createDb(function(db){createUser(function(user){if(user.isAuthorized("cart/products"){response.write(JSON.stringify(db.productsInCart(user.cartId())))response.end()})else{response.unauthorized()}})})}
multiple source events(local state)
![Page 25: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/25.jpg)
functionproductsInCart(request,response){createDb(function(db){createUser(function(user){if(user.isAuthorized("cart/products"){response.write(JSON.stringify(db.productsInCart(user.cartId())))response.end()})else{response.unauthorized()}})})}
multiple source events(local state)
![Page 26: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/26.jpg)
Event Emitter(local state)
varhttp=require("http")
varserver=http.createServer(function(request,response){response.writeHead(200,{"Content-Type":"plain/text"})response.write("HelloWorld\n")response.end()})
server.listen(8080)
console.log(">SERVERSTARTED")
01#hello_world/hello_world_server.js
![Page 27: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/27.jpg)
Event Emitter(local state)
varhttp=require("http")
varserver=http.createServer(function(request,response){response.writeHead(200,{"Content-Type":"plain/text"})response.write("HelloWorld\n")response.end()})
server.listen(8080)
console.log(">SERVERSTARTED")
01#hello_world/hello_world_server.js
![Page 28: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/28.jpg)
Event Emitter(local state)
varhttp=require("http")
varserver=http.createServer(function(request,response){response.writeHead(200,{"Content-Type":"plain/text"})response.write("HelloWorld\n")response.end()})
server.listen(8080)
console.log(">SERVERSTARTED")
01#hello_world/hello_world_server.js
![Page 29: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/29.jpg)
Event Emitter(local state)
varhttp=require("http")
varserver=http.createServer(function(request,response){response.writeHead(200,{"Content-Type":"plain/text"})response.write("HelloWorld\n")response.end()})
server.listen(8080)
console.log(">SERVERSTARTED")
01#hello_world/hello_world_server.js
![Page 30: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/30.jpg)
Event Emitter(local state)
coder@apollo:~/Work/src/node/examples$ node hello_world_server.js> SERVER STARTED
coder@apollo:~$ curl "http://localhost:8080/"Hello World
#1
#2
![Page 31: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/31.jpg)
Event Emitter(local state)
varserver=require("http").createServer()
server.on("request",function(request,response){console.log(">REQUESTSTARTED")request.on("end",function(){console.log(">REQUESTCLOSED")response.writeHead(200,{"Content-Type":"plain/text"})response.end("HelloWorld\n")server.close()})response.on("close",function(){console.log(">RESPONSECLOSED")})})
01#hello_world/hello_world_server_emitter.js
![Page 32: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/32.jpg)
Event Emitter(local state)
...
server.on("close",function(){console.log(">SERVERCLOSED")})
server.on("listening",function(){console.log(">SERVERSTARTED")})
server.listen(8080)
01#hello_world/hello_world_server_emitter.js
![Page 33: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/33.jpg)
Event Emitter(local state)
coder@apollo:~/Work/src/node/examples$ node hello_world_server.js> SERVER STARTED
coder@apollo:~$ curl "http://localhost:8080/"Hello World
> REQUEST STARTED> REQUEST CLOSED> SERVER CLOSED
#1
#2
#1
![Page 34: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/34.jpg)
why socomplicated?
![Page 35: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/35.jpg)
server.on("request",function(request,response){varchunks=[],output=fs.createWriteStream("./output")
request.on("data",function(chunk){chunks=forEachLine(chunks.concat(chunk),function(line){output.write(parseInt(line,10)*2)output.write("\n")})})
request.on("end",function(){response.writeHead(200,{"Content-Type":"plain/text"})response.end("OK\n")output.end()server.close()})})
data streams
02#proxy_stream/proxy_stream.js
![Page 36: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/36.jpg)
Event Emitter(local state)
coder@apollo:~/Work/src/node/examples$ node stream_doubler.js
coder@apollo:~$ curl "http://localhost:8080/" --data $'1\n2\n3\n'OK
coder@apollo:~/Work/src/node/examples$ cat output 246
#1
#2
#1
![Page 37: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/37.jpg)
why javascript?
• Friendly callbacks• ubiquitous (well known)• no I/o primitives• one language to rule them all
![Page 38: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/38.jpg)
mind shift #1
application
application
application
application
Web s
erver
web applications before: a web
server with some application logic
![Page 39: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/39.jpg)
mind shift #1
web applications after: an application
accessible over http
web s
erver
application
![Page 40: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/40.jpg)
mind shift #1
web applications after: an application
that can communicate and collaborate with
the worldw
eb s
erver
application
![Page 41: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/41.jpg)
mind shift #2
web applications before: stateful
• no easy to scale• no easy to reuse
M
c
v
![Page 42: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/42.jpg)
mind shift #2
web applications before: stateful
• no easy to scale• no easy to reuse
applicationstate
conversationstate
M
c
v
![Page 43: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/43.jpg)
mind shift #2
web applications before: stateful
• no easy to scale• no easy to reuse
M
c
v
tightly coupled
![Page 44: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/44.jpg)
mind shift #2
web applications after: stateless
• easy to scale• easy to reuse
http M
c
v
![Page 45: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/45.jpg)
mind shift #2
web applications after: stateless
• easy to scale• easy to reuse
http M
c
v
conversationstate
applicationstate
![Page 46: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/46.jpg)
mind shift #2
web applications after: stateless
• easy to scale• easy to reuse
http
M
http
web s
erver
statefullconnection
M
![Page 47: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/47.jpg)
no fluffjust stuff
![Page 48: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/48.jpg)
tic - tac - toe
![Page 49: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/49.jpg)
demo
tic - tac - toe
![Page 50: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/50.jpg)
INSTALL NPM(node packet manager)
coder@apollo:~/Work/src/node/examples$ curl http://npmjs.org/install.sh | sh...npm okIt worked
coder@apollo:~/Work/src/node/examples$ npm list | wc -l1776
coder@apollo:~/Work/src/node/examples$ npm install [email protected]@apollo:~/Work/src/node/examples$ npm install [email protected]@apollo:~/Work/src/node/examples$ npm install [email protected]@apollo:~/Work/src/node/examples$ npm install [email protected]
![Page 51: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/51.jpg)
static handler
varserver=connect.createServer(connect.logger({"buffer":true})).use("/",connect.router(function(resource){resource.get("/board",function(request,response,next){request.url="/board.html"next()})...}),connect.staticProvider({"root":path.join(__dirname,"static"),"cache":true}))
server.listen(port)
05#tictactoe/server.js
![Page 52: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/52.jpg)
game handler(generate board-id)
resource.post("/board",function(request,response){response.writeHead(200,{"Content-Type":"application/json"})uuid(function(boardId){response.end(JSON.stringify({"board":{"id":boardId}}))})})
05#tictactoe/server.js
![Page 53: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/53.jpg)
game handler(initial board/user)
resource.get("/board/:id",function(request,response){varboard=boards.get(request.params["id"])if(board===undefined){board=newBoard({"id":request.params["id"]})boards.add(board)}uuid(function(userId){varuser=board.user(userId)response.writeHead(200,{"Content-Type":"application/json"})response.end(JSON.stringify({"board":board,"user":user}))})})
05#tictactoe/server.js
![Page 54: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/54.jpg)
game handler(make your move)
resource.post("/board/:id",function(request,response){waitForBody(request,function(body){boards.get(request.params["id"]).move(JSON.parse(body))response.writeHead(204,{"Content-Type":"application/json"})response.end(JSON.stringify({"response":"ok"}))})})
05#tictactoe/server.js
![Page 55: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/55.jpg)
comet handler
varcomet=newFaye.NodeAdapter({"mount":"/comet","timeout":50})
varserver=connect.createServer(connect.logger({"buffer":true})).use("/comet",function(request,response,next){comet.handle(request,response)})...})
comet.attach(server)
05#tictactoe/server.js
![Page 56: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/56.jpg)
comet EVENTSon backbone events
varclient=comet.getClient()varboards=newBackbone.Collection
boards.bind("change",function(board){client.publish("/board-"+board.get("id"),board)})
05#tictactoe/server.js
![Page 57: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/57.jpg)
$(function(){
$.sammy(function(){this.get("",function(context){$.post("/board",function(response){context.redirect("#/board/"+response["board"]["id"])})})
...}).run()})
in browserrouting
05#tictactoe/static/board.html
![Page 58: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/58.jpg)
in browserrouting/start game
varcomet=newFaye.Client("/comet")vargame=newGame()
this.get("#/board/:id",function(context){game.start()$.get("/board/"+context.params["id"],function(response){game.set({"me":newUser(response.user)})game.set({"board":newBoard(response.board)})comet.connect()comet.subscribe("/board-"+context.params["id"],function(board){game.get("board").set(board)})})})
05#tictactoe/static/board.html
![Page 59: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/59.jpg)
in browsergame logic example
window.Game=Backbone.Model.extend({"initialize":function(){...game.get("board").bind("change",function(){if(this.isMyTurn()){returngame.trigger("make-your-move")}returngame.trigger("wait-for-move")})}})
05#tictactoe/static/js/application.js
![Page 60: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/60.jpg)
in browsergame logic example
game.bind("play-with-board",function(cells){buildBoard(['_'].concat(cells))})
game.bind("play-with-mark",function(mark){showPlayerMarker(cellMarksUrl[myMark=mark])})
game.bind("make-your-move",function(){$("#board").undelegate()$("#board").delegate("*[id^=cell]","mouseover",function(){$(this).data("cell").select()})$("#board").delegate("*[id^=cell]","mouseout",function(){$(this).data("cell").unselect()})...
05#tictactoe/static/js/application.js
![Page 61: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/61.jpg)
What aboutcpu bound
tasks?
![Page 62: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/62.jpg)
the forkbe with you
#!/bin/bash
forcountin`seq1100`;doecho$countsleep0.1done
03#long_running_jobs/long_running_job.sh
![Page 63: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/63.jpg)
the forkbe with you
varspawn=require("child_process").spawn,server=require("http").createServer()
server.on("request",function(request,response){varjob=spawn("./long_running_job.sh")
job.stdout.on("data",function(tick){response.write(tick)})
job.on("exit",function(){response.end()})})
03#long_running_jobs/long_running_server.js
![Page 64: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/64.jpg)
the forkbe with you
coder@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...
![Page 65: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/65.jpg)
the forkbe with you
coder@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...
![Page 66: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/66.jpg)
enter comet
watc
h
Mwatch
watch
spawn
watch
watc
h
watc
h
![Page 67: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/67.jpg)
enter comet
demo
![Page 68: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/68.jpg)
varport=8080
varserver=connect.createServer(connect.logger()).use("/comet",function(request,response){...}).use("/spawn",function(request,response){...}).use("/",connect.staticProvider({"root":path.join(__dirname,"static"),"cache":true}))
comet.attach(server)
server.listen(port)
static handler
04#progress/progress_server.js
![Page 69: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/69.jpg)
varcomet=newFaye.NodeAdapter({"mount":"/comet","timeout":50})
varserver=connect.createServer(connect.logger()).use("/comet",function(request,response,next){comet.handle(request,response)})...
comet handler
04#progress/progress_server.js
![Page 70: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/70.jpg)
varclient=comet.getClient(),jobCounter=0
varserver=connect.createServer(connect.logger()).use("/comet",function(request,response){...}).use("/spawn",function(request,response,next){varworker=spawn("./long_running_process.sh"),jobId=jobsCounter++response.writeHead(200,{"Content-Type":"plain/text"})response.end("OK\n")
worker.stdout.on("data",function(progress){client.publish("/job-progress",{"id":jobId,"progress":parseInt(progress.toString(),10)})})})
spawn handler
04#progress/progress_server.js
![Page 71: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/71.jpg)
in browser
<script>$(function(){varcomet=newFaye.Client("/comet")
comet.connect()comet.subscribe("/job-progress",function(job){$("#template").progressBar(job.id,job.progress)})})</script>
04#progress/static/index.html
![Page 72: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/72.jpg)
node.js is awesome but when should i use it?
![Page 73: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/73.jpg)
• chat/messaging• real-time applications• intelligent proxies• high concurrency applications• communication hubs• coordinators
when to use it?
![Page 74: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/74.jpg)
please tell me somethingbad aboutnode.js
![Page 75: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/75.jpg)
some warnings
• release stable 0.2.4 (young)• lots of stuffs to look at• lots of half backed stuffs• retro compatibility???• bad at handling static contents• hard to find organized and
authoritative informations
![Page 76: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/76.jpg)
![Page 77: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/77.jpg)
Questions?
![Page 78: Nodejs Explained with Examples](https://reader033.vdocuments.us/reader033/viewer/2022042521/554f4bdfb4c905524c8b4a02/html5/thumbnails/78.jpg)
gabriele [email protected]
twitter: @gabrielelana
this presentation code is onhttps://github.com/gabrielelana/node-examples