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

13,717 views
12,858 views

Published on

Increasingly we want to do more with the web and Internet applications we build. We have more features, more data, more users, more devices and all of it needs to be in real-time. With all of these demands how can we keep up? The answer is choosing a language and a platform that are optimized for the kind of architecture Internet and web applications really have. The traditional approach prioritises computation, assigning server resources before they are actually needed. JavaScript and Node.js both take an event driven approach only assigning resources to events as they happen. This allows us to make dramatic gains in performance and resource utilization while still having an environment which is fun and easy to program.

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

No Downloads
Views
Total views
13,717
On SlideShare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
206
Comments
0
Likes
19
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
  • A language for the Internet: Why JavaScript and Node.js is right for Internet Application

    1. A language for the Internet Why JavaScript and Node.js is right for Internet ApplicationsTom Hughes-Croucher@sh1mmer
    2. Me
    3. Scalable Server-Side Code with JavaScriptNode Up and Running Tom Hughes-Croucher
    4. Internet?
    5. She’s called Eleanor
    6. More features More usersMore devices More data
    7. Cost Stuff
    8. How do we cope withthe increase in demand?
    9. Internet Applications
    10. How about search?
    11. Browser Server Spidering... The Web
    12. Would take forever!
    13. Browser Front-end Server Database
    14. Client → Server Computation
    15. Client → Server Server → DB Computation Computation
    16. “Traditional” Approach
    17. Server
    18. Request
    19. U L LF
    20. Event-driven Approach
    21. Place-holder
    22. Shared WorkResources
    23. Welcome to Node.js
    24. 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
    25. $Enki:~ $ node
    26. $Enki:~ $ node>3>2>1false> true == 1true> true === 1false
    27. > 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:~ $
    28. 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)
    29. 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/);
    30. var http = require(http);//include the http library
    31. 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
    32. 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
    33. 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
    34. Why is Node.js suited to Internet Apps?
    35. What is the event loop?
    36. Multi-tasking,one thing at a time.
    37. 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/);
    38. 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
    39. 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
    40. 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
    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/); Step 2. Do we have active listeners? listeners: server.request -> function Yes! Wait for listeners.
    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 3. Event Calls
    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 3. request is called. Since listeners: server.request -> function Call function
    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 3. Loop! (go to Step 2.)
    45. Breaking the event loop
    46. 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);
    47. Why non-blocking matters
    48. var result =db.query("select * from T");// use result
    49. What are wewaiting for?
    50. "Blocking" is as bad as stopping
    51. Event Loop vs. Threads
    52. PHP8mb 8gb ram 8000/8 = 1000~1000 users per machine
    53. Node.js TCP = 2kb HTTP = 6kb 8gb ram 8000/0.006 = 1.3m1.3m/2 ~ 650k users
    54. Apache vs NGINXconcurrency × reqs/sechttp://blog.webfaction.com/a-little-holiday-present
    55. Apache vs NGINXconcurrency × memoryhttp://blog.webfaction.com/a-little-holiday-present
    56. Node.js is designed forcommunication, just like your applications
    57. Thanks! Questions?Tom Hughes-Croucher@sh1mmer

    ×