A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

6,546 views
6,445 views

Published on

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

No Downloads
Views
Total views
6,546
On SlideShare
0
From Embeds
0
Number of Embeds
243
Actions
Shares
0
Downloads
212
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

    1. 1. A language for the Internet Why JavaScript and Node.js is right for Internet ApplicationsTom Hughes-Croucher@sh1mmer
    2. 2. Me(@sh1mmer)
    3. 3. Scalable Server-Side Code with JavaScriptNode Up and Running Tom Hughes-Croucher
    4. 4. A brief aside.A small lecture on biology
    5. 5. The common tree shrew
    6. 6. Diana Monkey
    7. 7. Back to your feature presentation.
    8. 8. Internet?
    9. 9. She’s called Eleanor
    10. 10. More features More usersMore devices More data
    11. 11. Cost Stuff
    12. 12. How do we cope withthe increase in demand?
    13. 13. Internet Applications
    14. 14. How about search?
    15. 15. Browser Server Spidering... The Web
    16. 16. Would take forever!
    17. 17. Browser Front-end ServerComputation Database
    18. 18. Computational ComputingClient → Server Computation
    19. 19. Internet ComputingClient → Server Server → DB Computation Computation
    20. 20. “Traditional” Approach
    21. 21. Server
    22. 22. Request
    23. 23. U L LF
    24. 24. Event-driven Approach
    25. 25. Place-holder
    26. 26. Shared WorkResources
    27. 27. Welcome to Node.js
    28. 28. Node.js?• Server Side JavaScript runtime• Built on top of V8 JavaScript engine from Google Chrome• Non-blocking I/O APIs• Easy to extend APIs and modules
    29. 29. $Enki:~ $ node
    30. 30. $Enki:~ $ node>3>2>1false> true == 1true> true === 1false
    31. 31. > console.log(Hello World);Hello World> .help.clear Break, and also clear the local context..exit Exit the prompt.help Show repl options> .clearClearing context...> .exitEnki:~ $
    32. 32. Enki:~ $ node> var foo = "bar";> foo;bar> .clearClearing context...> fooReferenceError: foo is not defined at [object Context]:1:1 at Interface.<anonymous> (repl:98:19) at Interface.emit (events:27:15) at Interface._ttyWrite (readline:295:12) at Interface.write (readline:132:30) at Stream.<anonymous> (repl:79:9) at Stream.emit (events:27:15) at IOWatcher.callback (net:489:16)
    33. 33. var http = require(http);http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);}).listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/);
    34. 34. var http = require(http);//include the http library
    35. 35. http.createServer(function (req, res) {}).listen(8124, "127.0.0.1");//create an http server//when ‘stuff’ happens call this anonymous function//listen on port 8124 of the IP 127.0.0.1
    36. 36. http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);})//when ‘stuff’ happens my function fires//I get a request object and a response object//I write to the response object header//HTTP status 200 and content-type ‘text/plain’//close the response with the body://Hello World
    37. 37. console.log(Server running at http://127.0.0.1:8124/);//write Server is running at http://127.0.0.1:8124///to the console
    38. 38. Why is Node.js suited to Internet Apps?
    39. 39. What is the event loop?
    40. 40. Multi-tasking,one thing at a time.
    41. 41. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/);
    42. 42. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 1. Evaluate Main
    43. 43. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 1. variables: http -> http module server -> http server listeners: server.request -> function
    44. 44. var http = require(http); server = http.createServer();* server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn); }); server.listen(8124, "127.0.0.1"); console.log(Server running at http://127.0.0.1:8124/); Step 2. Event Loop
    45. 45. var http = require(http); server = http.createServer();* server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn); }); server.listen(8124, "127.0.0.1"); console.log(Server running at http://127.0.0.1:8124/); Step 2. Do we have active listeners? listeners: server.request -> function Yes! Wait for listeners.
    46. 46. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 3. Event Calls
    47. 47. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 3. request is called. Since listeners: server.request -> function Call function
    48. 48. var http = require(http);server = http.createServer();server.on(request, function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);});server.listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/); Step 3. Loop! (go to Step 2.)
    49. 49. Breaking the event loop
    50. 50. EE = require(events).EventEmitter;ee = new EE();die = false;ee.on(die, function() { die = true;});setTimeout(function() { ee.emit(die);}, 100);while(!die) {}console.log(done);
    51. 51. Why non-blocking matters
    52. 52. var result =db.query("select * from T");// use result
    53. 53. What are wewaiting for?
    54. 54. "Blocking" is as bad as stopping
    55. 55. Event Loop vs. Threads
    56. 56. PHP8mb 8gb ram 8000/8 = 1000~1000 users per machine
    57. 57. Node.js TCP = 2kb HTTP = 6kb 8gb ram 8000/0.006 = 1.3m1.3m/2 ~ 650k users
    58. 58. Apache vs NGINXconcurrency × reqs/sechttp://blog.webfaction.com/a-little-holiday-present
    59. 59. Apache vs NGINXconcurrency × memoryhttp://blog.webfaction.com/a-little-holiday-present
    60. 60. Node.js is designed forcommunication, just like your applications
    61. 61. Thanks!
    62. 62. Follow me @sh1mmerTom Hughes-Croucher@sh1mmer

    ×