node.js A quick tour                15.02.2011 (v4)
About • Felix Geisendörfer • Berlin, Germany • Debuggable Ltd
@felixge
File uploading & processing as an infrastructure     service for web & mobile applications.
Core Contributor                    &              Module Authornode-mysql                      node-formidable
Audience?
Server-side JavaScript
Server-side JavaScript• Netscape LiveWire (1996)• Rhino (1997)• 50+ plattforms since then
What was the problem?• Slow engines• Lack of interest in JS until ~2005• Better competing plattforms
Why JavaScript?     3 Reasons
#1 - The good parts
V8 (Chrome)SpiderMonkey (Firefox)     JavaScriptCore (Safari)      #2 - JS Engine Wars       Chakra (IE9)        Carakan (...
#3 - No I/O in the core
Nodes goal is to provide an easy way to build        scalable network programs.
Node.js• Created by Ryan Dahl• Google’s V8 JavaScript engine (no DOM)• Module system, I/O bindings, Common Protocols
Installing$ git clone git://github.com/ry/node.git$ cd node$ ./configure$ make install
Hello World$ cat test.jsconsole.log(Hello World);$ node test.jsHello World
Ingredients         libeio            libevc-ares                      V8             http_parser
Philosophy• Just enough core-library to do I/O• Non-blocking• Close to the underlaying system calls
Non-blocking I/O
Blocking I/Ovar a = db.query(SELECT A);console.log(result a:, a);var b = db.query(SELECT B);console.log(result b:, b);    ...
Non-Blocking I/Odb.query(SELECT A, function(result) {  console.log(result a:, result);});db.query(SELECT B, function(resul...
Non-Blocking I/O• Offload most things to the kernel and poll  for changes (select, epoll, kqueue, etc.)• Thread pool for an...
Single Threadedvar a = [];function f() {  a.push(1);  a.push(2);}setTimeout(f, 10);setTimeout(f, 10);
Single Threadedvar a = [];function f() {       • [1, 1, 2, 2] ?  a.push(1);  a.push(2);}                    • [1, 2, 1, 2]...
Single Threadedvar a = [];function f() {       • [1, 1, 2, 2]  a.push(1);  a.push(2);}                    • [1, 2, 1, 2]se...
API Overview
CommonJS Modules$ cat hello.jsexports.world = function() {   return Hello World;};$ cat main.jsvar hello = require(./hello...
Child processes$ cat child.jsvar spawn = require(child_process).spawn;var cmd = echo hello; sleep 1; echo world;;var child...
Http Server$ cat http.jsvar http = require(http);http.createServer(function(req, res) {  setTimeout(function() {    res.wr...
Tcp Server$ cat tcp.jsvar tcp = require(tcp);tcp.createServer(function(socket) {  socket    .on(connect, function() {     ...
DNS$ cat dns.jsvar dns = require(dns);dns.resolve(nodejs.org, function(err, addresses) { console.log(addresses);});$ node ...
Watch File$ cat watch.jsvar fs = require(fs);fs.watchFile(__filename, function() {  console.log(You changed me!);  process...
And much more• UDP            • Buffer• Crypto         • Script• Assert         • EcmaScript5           ...
Suitable Applications
Suitable Applications• Singe-page apps• Real time• Crawlers
More Applications• Async chaining of unix tools• Streaming• File uploading
Interesting projects
Package management
Web Frameworks• Express.js (Sinatra clone)• Fab.js (Mind-bending & awesome)
DOM• node-htmlparser• jquery• node.io
WebSockets Socket.IO
1171 modules in npm
Ready for production?
Ready for production?• 0.4.2 was released on March 3rd• API has settled down for most parts• Very few bugs, but YMMV
Things that suck
Things that suck• Stack traces are lost at the event loop  boundary• Utilizing multiple cores requires multiple  processes...
Community
Benevolent Dictator For Life          Ryan Dahl
Community• Mailing list (nodejs, nodejs-dev)• IRC (#node.js)
Hosting
Questions?✎   @felixge / felix@debuggable.com
Bonus Slides
Speed
Speedvar http = require(’http’)var b = new Buffer(1024*1024);http.createServer(function (req, res) {  res.writeHead(200); ...
Speed100 concurrent clients1 megabyte responsenode 822 req/secnginx 708thin 85mongrel 4(bigger is better)                 ...
Speed• NGINX peaked at 4mb of memory• Node peaked at 60mb• Very special circumstances                                  by ...
Questions?✎   @felixge / felix@debuggable.com
Upcoming SlideShare
Loading in...5
×

Nodejs - A quick tour (v5)

2,952

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,952
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
41
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Nodejs - A quick tour (v5)

  1. 1. node.js A quick tour 15.02.2011 (v4)
  2. 2. About • Felix Geisendörfer • Berlin, Germany • Debuggable Ltd
  3. 3. @felixge
  4. 4. File uploading & processing as an infrastructure service for web & mobile applications.
  5. 5. Core Contributor & Module Authornode-mysql node-formidable
  6. 6. Audience?
  7. 7. Server-side JavaScript
  8. 8. Server-side JavaScript• Netscape LiveWire (1996)• Rhino (1997)• 50+ plattforms since then
  9. 9. What was the problem?• Slow engines• Lack of interest in JS until ~2005• Better competing plattforms
  10. 10. Why JavaScript? 3 Reasons
  11. 11. #1 - The good parts
  12. 12. V8 (Chrome)SpiderMonkey (Firefox) JavaScriptCore (Safari) #2 - JS Engine Wars Chakra (IE9) Carakan (Opera)
  13. 13. #3 - No I/O in the core
  14. 14. Nodes goal is to provide an easy way to build scalable network programs.
  15. 15. Node.js• Created by Ryan Dahl• Google’s V8 JavaScript engine (no DOM)• Module system, I/O bindings, Common Protocols
  16. 16. Installing$ git clone git://github.com/ry/node.git$ cd node$ ./configure$ make install
  17. 17. Hello World$ cat test.jsconsole.log(Hello World);$ node test.jsHello World
  18. 18. Ingredients libeio libevc-ares V8 http_parser
  19. 19. Philosophy• Just enough core-library to do I/O• Non-blocking• Close to the underlaying system calls
  20. 20. Non-blocking I/O
  21. 21. Blocking I/Ovar 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)
  22. 22. Non-Blocking I/Odb.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)
  23. 23. Non-Blocking I/O• Offload most things to the kernel and poll for changes (select, epoll, kqueue, etc.)• Thread pool for anything else
  24. 24. Single Threadedvar a = [];function f() { a.push(1); a.push(2);}setTimeout(f, 10);setTimeout(f, 10);
  25. 25. Single Threadedvar a = [];function f() { • [1, 1, 2, 2] ? a.push(1); a.push(2);} • [1, 2, 1, 2] ?setTimeout(f, 10); • BAD ACCESS ?setTimeout(f, 10);
  26. 26. Single Threadedvar a = [];function f() { • [1, 1, 2, 2] a.push(1); a.push(2);} • [1, 2, 1, 2]setTimeout(f, 10); • BAD ACCESSsetTimeout(f, 10);
  27. 27. API Overview
  28. 28. CommonJS Modules$ cat hello.jsexports.world = function() { return Hello World;};$ cat main.jsvar hello = require(./hello);console.log(hello.world());$ node main.jsHello World
  29. 29. 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"hellon"# 1 sec delay"worldnn"
  30. 30. 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!
  31. 31. 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!
  32. 32. DNS$ cat dns.jsvar dns = require(dns);dns.resolve(nodejs.org, function(err, addresses) { console.log(addresses);});$ node dns.js[ 8.12.44.238 ]
  33. 33. 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!
  34. 34. And much more• UDP • Buffer• Crypto • Script• Assert • EcmaScript5 ...
  35. 35. Suitable Applications
  36. 36. Suitable Applications• Singe-page apps• Real time• Crawlers
  37. 37. More Applications• Async chaining of unix tools• Streaming• File uploading
  38. 38. Interesting projects
  39. 39. Package management
  40. 40. Web Frameworks• Express.js (Sinatra clone)• Fab.js (Mind-bending & awesome)
  41. 41. DOM• node-htmlparser• jquery• node.io
  42. 42. WebSockets Socket.IO
  43. 43. 1171 modules in npm
  44. 44. Ready for production?
  45. 45. Ready for production?• 0.4.2 was released on March 3rd• API has settled down for most parts• Very few bugs, but YMMV
  46. 46. Things that suck
  47. 47. 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
  48. 48. Community
  49. 49. Benevolent Dictator For Life Ryan Dahl
  50. 50. Community• Mailing list (nodejs, nodejs-dev)• IRC (#node.js)
  51. 51. Hosting
  52. 52. Questions?✎ @felixge / felix@debuggable.com
  53. 53. Bonus Slides
  54. 54. Speed
  55. 55. Speedvar 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
  56. 56. Speed100 concurrent clients1 megabyte responsenode 822 req/secnginx 708thin 85mongrel 4(bigger is better) by Ryan Dahl
  57. 57. Speed• NGINX peaked at 4mb of memory• Node peaked at 60mb• Very special circumstances by Ryan Dahl
  58. 58. Questions?✎ @felixge / felix@debuggable.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×