0
A practical introductionFelix Geisendörfer                Munich Node.js User Group 01.12.2011 (v2)
@felixgeTwitter / GitHub / IRC    Felix Geisendörfer     (Berlin, Germany)
Node.js in Munich?
Used node before?
Node in production?
History
Feb 16, 2009Ryan Dahl starts the node project (first commit)
~June, 2009Discovered node.js (v0.0.6)
transloadit.com
Core Contributor                    &              Module Authornode-mysql                      node-formidable           ...
Sep 29, 2009Isaac Schlueter starts the npm package manager                  (first commit)
Nov 7, 2009Ryan’s talk at JSConf.EU gets people excited about node
Today
#1Most watched repository on GitHub
...
0.6.3 was released 6 days ago (Nov 25)
Companies using node• LinkedIn (Mobile Web App)• Ebay (Data retrieval gateway)• GitHub (for Downloads)• Palm/HP (in WebOS)...
Installing$ git clone git://github.com/joyent/node.git$ cd node$ git checkout v0.6.0$ ./configure$ sudo make install     (...
Hello Worldhello.jsconsole.log(Hello World);
Hello Worldhello.jsconsole.log(Hello World);$
Hello Worldhello.jsconsole.log(Hello World);$ node
Hello Worldhello.jsconsole.log(Hello World);$ node hello.js
Hello Worldhello.jsconsole.log(Hello World);$ node hello.jsHello World
Hello World (REPL)
Hello World (REPL) $
Hello World (REPL) $ node
Hello World (REPL) $ node >
Hello World (REPL) $ node > console.log(Hello World)
Hello World (REPL) $ node > console.log(Hello World) Hello World
Http Serverhttp_server.jsvar http = require(http);var server = http.createServer(function(req, res) {  res.end(Hi, how are...
Http Serverhttp_server.jsvar http = require(http);var server = http.createServer(function(req, res) {  res.end(Hi, how are...
Http Serverhttp_server.jsvar http = require(http);var server = http.createServer(function(req, res) {  res.end(Hi, how are...
“Come on, server side JS has been around since        1996”
What is so special about node?
Speed
Speed• Node can do ~6000 http requests / sec per  CPU core (hello world, 1kb response,)• It is no problem to handle thousa...
V8 JavaScript Engine
V8 JavaScript Engine• Developed by Google in Aarhus, Denmark  for Google Chrome• Translates JavaScript in Assembly Code• C...
V8 JavaScript Engine• You can expect to be within ~10x of C  performance usually• Certain code can run within 2-3x of C• G...
Non-Blocking I/O
Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var ...
Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var ...
Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var ...
Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var ...
Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) {  console.log(Read ...
Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) {  console.log(Read ...
Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) {  console.log(Read ...
Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) {  console.log(Read ...
Blocking I/ORead one.txt (20ms)                                Read two.txt (10ms)              Total duration (30ms)
Non-Blocking I/ORead one.txt (20ms)Read two.txt (10ms)      Total duration (20ms)
Non-Blocking I/O• Close to ideal for high concurrency / high  throughput, single execution stack• Forces you to write more...
WebSockets  (Push)
WebSockets•   Persistent connection between browser/server•   Very hard / awkward to do on traditional stacks•   Hard to s...
Socket.IO (community module)       •   WebSocket       •   Adobe® Flash® Socket       •   AJAX long polling       •   AJAX...
Streams
“Streams are to time as arrays are to space.”                    -- Jed Schmidt @ JSConf.eu 2010
Streams in node.js•   Readable•   Writable•   Both
Streamsvar http = require(http);var server = http.createServer(function(req, res) {  req.on(data, console.log);});server.l...
Streamsvar http = require(http);var server = http.createServer(function(req, res) {  req.on(data, console.log);});server.l...
Streamsvar http = require(http);var spawn = require(child_process).spawn;http.createServer(function(req, res) {  var param...
On Githubfelixge/node-convert-example
NPM package manager
NPM• Puts dependencies in the right place, then  gets out of your way• No modification of a global load path  (require.path...
So what is node good        for?
Use cases• WebSockets/Push applications• Proxying data streams• Backend for single page apps
Use cases• Spawning other programs (processes) to do  work / IPC• Parallelizing I/O
So what is node not     good for?
Anti-Use cases• Hard Realtime Systems• Number crunching / huge in-memory  datasets• (CRUD apps)
Join the Community• Mailing list (nodejs, nodejs-dev)• IRC (#node.js) - 700+ User online
Thank you!
Questions?   @felixge
Thank you!
Bonus Slide
What’s next?• Domains• Improved Stream API
Node.js  - A practical introduction (v2)
Node.js  - A practical introduction (v2)
Node.js  - A practical introduction (v2)
Upcoming SlideShare
Loading in...5
×

Node.js - A practical introduction (v2)

5,760

Published on

Talk given at the munich node.js user group

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

No Downloads
Views
Total Views
5,760
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
275
Comments
0
Likes
22
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • #24 on the mailing list\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Mention difference between node / other platforms where you’re implicitly embedded inside a webserver\n
  • Mention difference between node / other platforms where you’re implicitly embedded inside a webserver\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • If that is not fast enough for what you’re doing, use C\n
  • \n
  • \n
  • \n
  • \n
  • Now you may say:\n- This is terrible\n- I have to write 7 lines instead of 5\n- And it’s in the wrong order\n\n
  • Now you may say:\n- This is terrible\n- I have to write 7 lines instead of 5\n- And it’s in the wrong order\n\n
  • Now you may say:\n- This is terrible\n- I have to write 7 lines instead of 5\n- And it’s in the wrong order\n\n
  • \n
  • \n
  • \n
  • A keen observer may note that the same is achievable with threads\n* But how many of you think threading is easier than making an AJAX call\n* Threads require significant more resources than the non-blocking I/O node is doing\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 10 lines of code\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "Node.js - A practical introduction (v2)"

    1. 1. A practical introductionFelix Geisendörfer Munich Node.js User Group 01.12.2011 (v2)
    2. 2. @felixgeTwitter / GitHub / IRC Felix Geisendörfer (Berlin, Germany)
    3. 3. Node.js in Munich?
    4. 4. Used node before?
    5. 5. Node in production?
    6. 6. History
    7. 7. Feb 16, 2009Ryan Dahl starts the node project (first commit)
    8. 8. ~June, 2009Discovered node.js (v0.0.6)
    9. 9. transloadit.com
    10. 10. Core Contributor & Module Authornode-mysql node-formidable + 30 other modules
    11. 11. Sep 29, 2009Isaac Schlueter starts the npm package manager (first commit)
    12. 12. Nov 7, 2009Ryan’s talk at JSConf.EU gets people excited about node
    13. 13. Today
    14. 14. #1Most watched repository on GitHub
    15. 15. ...
    16. 16. 0.6.3 was released 6 days ago (Nov 25)
    17. 17. Companies using node• LinkedIn (Mobile Web App)• Ebay (Data retrieval gateway)• GitHub (for Downloads)• Palm/HP (in WebOS)• Yahoo! Mail• Dow Jones & Company (for WJS social site)• Rackspace (Cloudkick monitoring)• Voxxer (Push to Talk mobile app)
    18. 18. Installing$ git clone git://github.com/joyent/node.git$ cd node$ git checkout v0.6.0$ ./configure$ sudo make install (windows users: download node.exe)
    19. 19. Hello Worldhello.jsconsole.log(Hello World);
    20. 20. Hello Worldhello.jsconsole.log(Hello World);$
    21. 21. Hello Worldhello.jsconsole.log(Hello World);$ node
    22. 22. Hello Worldhello.jsconsole.log(Hello World);$ node hello.js
    23. 23. Hello Worldhello.jsconsole.log(Hello World);$ node hello.jsHello World
    24. 24. Hello World (REPL)
    25. 25. Hello World (REPL) $
    26. 26. Hello World (REPL) $ node
    27. 27. Hello World (REPL) $ node >
    28. 28. Hello World (REPL) $ node > console.log(Hello World)
    29. 29. Hello World (REPL) $ node > console.log(Hello World) Hello World
    30. 30. Http Serverhttp_server.jsvar http = require(http);var server = http.createServer(function(req, res) { res.end(Hi, how are you?);});server.listen(8080);
    31. 31. Http Serverhttp_server.jsvar http = require(http);var server = http.createServer(function(req, res) { res.end(Hi, how are you?);});server.listen(8080);$ node http_server.js
    32. 32. Http Serverhttp_server.jsvar http = require(http);var server = http.createServer(function(req, res) { res.end(Hi, how are you?);});server.listen(8080);$ node http_server.js $ curl localhost:8080 Hi, how are you?
    33. 33. “Come on, server side JS has been around since 1996”
    34. 34. What is so special about node?
    35. 35. Speed
    36. 36. Speed• Node can do ~6000 http requests / sec per CPU core (hello world, 1kb response,)• It is no problem to handle thousands of concurrent connections which are moderately active
    37. 37. V8 JavaScript Engine
    38. 38. V8 JavaScript Engine• Developed by Google in Aarhus, Denmark for Google Chrome• Translates JavaScript in Assembly Code• Crankshaft JIT (enabled in 0.6.0 by default)
    39. 39. V8 JavaScript Engine• You can expect to be within ~10x of C performance usually• Certain code can run within 2-3x of C• Getting faster all the time
    40. 40. Non-Blocking I/O
    41. 41. Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var two = fs.readFileSync(two.txt, utf-8);console.log(Read file two);
    42. 42. Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var two = fs.readFileSync(two.txt, utf-8);console.log(Read file two); $ node read_file_sync.js
    43. 43. Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var two = fs.readFileSync(two.txt, utf-8);console.log(Read file two); $ node read_file_sync.js Read file one
    44. 44. Blocking I/Oread_file_sync.jsvar fs = require(fs);var one = fs.readFileSync(one.txt, utf-8);console.log(Read file one);var two = fs.readFileSync(two.txt, utf-8);console.log(Read file two); $ node read_file_sync.js Read file one Read file two
    45. 45. Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) { console.log(Read file one);});fs.readFile(two.txt, utf-8, function(err, data) { console.log(Read file two);});
    46. 46. Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) { console.log(Read file one);});fs.readFile(two.txt, utf-8, function(err, data) { console.log(Read file two);}); $ node read_file_async.js
    47. 47. Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) { console.log(Read file one);});fs.readFile(two.txt, utf-8, function(err, data) { console.log(Read file two);}); $ node read_file_async.js Read file two
    48. 48. Non-Blocking I/Oread_file_async.jsvar fs = require(fs);fs.readFile(one.txt, utf-8, function(err, data) { console.log(Read file one);});fs.readFile(two.txt, utf-8, function(err, data) { console.log(Read file two);}); $ node read_file_async.js Read file two Read file one
    49. 49. Blocking I/ORead one.txt (20ms) Read two.txt (10ms) Total duration (30ms)
    50. 50. Non-Blocking I/ORead one.txt (20ms)Read two.txt (10ms) Total duration (20ms)
    51. 51. Non-Blocking I/O• Close to ideal for high concurrency / high throughput, single execution stack• Forces you to write more efficient code by parallelizing your I/O• Feels pretty much like AJAX in the browser
    52. 52. WebSockets (Push)
    53. 53. WebSockets• Persistent connection between browser/server• Very hard / awkward to do on traditional stacks• Hard to scale on traditional stacks
    54. 54. Socket.IO (community module) • WebSocket • Adobe® Flash® Socket • AJAX long polling • AJAX multipart streaming • Forever Iframe • JSONP PollingChooses most capable transport at runtime!
    55. 55. Streams
    56. 56. “Streams are to time as arrays are to space.” -- Jed Schmidt @ JSConf.eu 2010
    57. 57. Streams in node.js• Readable• Writable• Both
    58. 58. Streamsvar http = require(http);var server = http.createServer(function(req, res) { req.on(data, console.log);});server.listen(8080);
    59. 59. Streamsvar http = require(http);var server = http.createServer(function(req, res) { req.on(data, console.log);});server.listen(8080);$ node stream.js &$ curl -F file=@stream.js localhost:8080------------------------------41e92562223eContent-Disposition: form-data; name="file"; filenameContent-Type: application/octet-streamvar http = require(http);var server = http.createServer(function(req, res) {
    60. 60. Streamsvar http = require(http);var spawn = require(child_process).spawn;http.createServer(function(req, res) { var params = req.url.split(/); var args = [params[1], -resize, params[2], -]; var convert = spawn(convert, args); convert.stdout.pipe(res);}).listen(8080);
    61. 61. On Githubfelixge/node-convert-example
    62. 62. NPM package manager
    63. 63. NPM• Puts dependencies in the right place, then gets out of your way• No modification of a global load path (require.paths is gone in 0.6.x)• 5369+ Modules in npm, > 10 new modules / day
    64. 64. So what is node good for?
    65. 65. Use cases• WebSockets/Push applications• Proxying data streams• Backend for single page apps
    66. 66. Use cases• Spawning other programs (processes) to do work / IPC• Parallelizing I/O
    67. 67. So what is node not good for?
    68. 68. Anti-Use cases• Hard Realtime Systems• Number crunching / huge in-memory datasets• (CRUD apps)
    69. 69. Join the Community• Mailing list (nodejs, nodejs-dev)• IRC (#node.js) - 700+ User online
    70. 70. Thank you!
    71. 71. Questions? @felixge
    72. 72. Thank you!
    73. 73. Bonus Slide
    74. 74. What’s next?• Domains• Improved Stream API
    1. A particular slide catching your eye?

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

    ×