node.js: Javascript's in your backend

19,251
-1

Published on

Has the traditional intro to event looped servers (thanks Ryan!) with a couple of examples of why I think node.js is particularly exciting today. Code for the demos can be found at https://github.com/davidpadbury/node-intro.

Published in: Technology
5 Comments
26 Likes
Statistics
Notes
No Downloads
Views
Total Views
19,251
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
169
Comments
5
Likes
26
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • http://www.flickr.com/photos/loshak/530449376/\n
  • \n
  • \n
  • http://www.flickr.com/photos/rtv/256102280/\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • http://www.flickr.com/photos/gabrielleinaustin/2454197457/\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • http://www.flickr.com/photos/awcole72/1936225899/\n
  • \n
  • \n
  • \n
  • http://www.flickr.com/photos/freeed/5379562284/\n
  • \n
  • http://www.flickr.com/photos/illumiquest/2749137895/\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
  • http://www.flickr.com/photos/mr-numb/4753899767/\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • http://www.flickr.com/photos/bcymet/3292063588/\n
  • \n
  • http://www.flickr.com/photos/el_momento_i_sitio_apropiados/5166623452/\n
  • http://www.flickr.com/photos/angelinawb/266143527/\n
  • http://www.flickr.com/photos/wongjunhao/4285302488/\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • http://www.flickr.com/photos/pasukaru76/5268559005/\n
  • http://www.flickr.com/photos/seven13avenue/2758702332/\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • http://www.flickr.com/photos/hometownzero/136283072/\n
  • http://www.flickr.com/photos/22326055@N06/3444756625/\n
  • \n
  • http://www.flickr.com/photos/kylesteeddesign/4507065826/\n
  • http://www.flickr.com/photos/state_library_south_australia/3925493310/\n
  • \n
  • \n
  • \n
  • \n
  • node.js: Javascript's in your backend

    1. 1. node.jsJavaScript’s in your backend David Padbury http://davidpadbury.com @davidpadbury
    2. 2. Programming 101
    3. 3. static void Main(){ int result = Add(2, 2); Console.WriteLine("2+2 = {0}", result);}static int Add(int n1, int n2){ return n1 + n2;}
    4. 4. static void Main(){ string name = GetName(); Console.WriteLine("Hi {0}", name);}static string GetName(){ return Console.ReadLine();}
    5. 5. Looks pretty much the same?
    6. 6. static void Main() Normal Method Call{ int result = Add(2, 2); Console.WriteLine("2+2 = {0}", result);}static int Add(int n1, int n2){ return n1 + n2;} Does some stuff in processor cache or RAM
    7. 7. static void Main() Normal Method Call{ string name = GetName(); Console.WriteLine("Hi {0}", name);}static string GetName(){ return Console.ReadLine();} Blocks until ready
    8. 8. What’s the big deal?Well, let’s think about what we do in a web server...
    9. 9. public ActionResult View(int id){ var product = northwindDataContext.Get(id); return View(product);} Blocking Network Call (IO)
    10. 10. Think about what we really do on a web server...
    11. 11. Call Databases Grab Files Think about what we really do on a web server...Pull from caches Wait on other connections
    12. 12. It’s all I/O
    13. 13. CPU Cycles L1 3 L2 14 RAM 250 Disk 41,000,000Network 240,000,000 0 75,000,000 150,000,000 225,000,000 300,000,000 http://nodejs.org/jsconf.pdf
    14. 14. Comparatively, I/O pretty much takes forever...
    15. 15. And yet, we write code exactly the same.
    16. 16. “we’re doing it wrong” - Ryan Dahl, node.js creatorhttp://www.yuiblog.com/blog/2010/05/20/video-dahl/
    17. 17. So I can guess what you’re thinking, “What about multi-threading?”
    18. 18. Yep, mainstream web servers like Apache and IIS* use a Thread Per Connection* Well, technically IIS doesn’t quite have a thread per connection as there’s some kernel level stuff which will talk to IIS/ ASP.NET depending on your configuration and it’s all quite complicated. But for the sake of this presentation we’ll say it’s a thread per connection as it’s pretty darn close in all practical terms. If you’d like to talk about this more feel free to chat to me afterwards, I love spending my spare time talking about threading in IIS. HONEST.
    19. 19. Threading ain’t freeContext Switching Execution Stacks take Memory
    20. 20. Sure - but what else?
    21. 21. An Event LoopUse a single thread - do little pieces of work, and do ‘em quickly
    22. 22. With an event loop, you ask itto do something and it’ll get back to you when it’s done
    23. 23. But, a single thread?
    24. 24. nginx is event based (higher is better)http://blog.webfaction.com/a-little-holiday-present
    25. 25. (lower is better)http://blog.webfaction.com/a-little-holiday-present
    26. 26. Being event based is actually a pretty good way of writing heavily I/O bound servers
    27. 27. So why don’t we?
    28. 28. We’d have to completely change how we write code public ActionResult View(int id) { var product = northwindDataContext.Get(id); return View(product); Blocking Call }
    29. 29. To something where we could easily write non-blocking code public void View(HttpResponse response, int id) { Non-blocking Call northwindDataContext.Get(id, (product) => { response.View(product); }); } Anonymous Function Callback
    30. 30. CMost old school languages can’t do this
    31. 31. *cough*Java *cough*
    32. 32. Even if we had a language that madewriting callbacks easy (like C#), we’d need a platform that had minimal to no blocking I/O operations.
    33. 33. If only we had a language which was designed to be inherently single threaded, had first class functions and closures built in, and had no preconceived notions about I/O? Wouldn’t it also be really handy if half* of the developers in the world already knew it? (you can probably guess where this is going...) *The guy speaking completely made that up for the sake of this slide. but he’s pretty sure there are quite a few
    34. 34. http://nodejs.org/
    35. 35. Node.js is a set of JavaScript bindings forwriting high-performance network servers
    36. 36. With the goal of making developing high- performance network servers easy
    37. 37. Built on Google’s V8 js engine (so it’s super quick)
    38. 38. Exposes only non-blocking I/O API’s
    39. 39. Stops us writing code that behaves badly
    40. 40. DemoBasic node.js
    41. 41. console.log(Hello);setTimeout(function() { console.log(World);}, 2000);
    42. 42. Prints Immediately $node app.js Hello Waits two seconds World $ Exits as there’s nothing left to do
    43. 43. Node comes with a large set of libraries
    44. 44. Node comes with a large set of libraries Timers Processes Events BuffersStreams Crypto File System REPL Net HTTP HTTPS TLS/SSL DNS UDP/Datagram
    45. 45. Libraries are ‘imported’ using the require function
    46. 46. Just a function call var http = require(http), fs = require(fs);Returns object exposing the API
    47. 47. DemoRequire and standard modules
    48. 48. Every request is just a callbackvar http = require(http);var server = http.createServer(function(req, res) { res.writeHead(200, { Content-Type: text/plain }); res.end(Hello from node.js!);});server.listen(3000);console.log(Server started on 3000);
    49. 49. var fs = require(fs), Doesn’t block to read file http = require(http);var server = http.createServer(function(req, res) { fs.readFile(./data.txt, function(err, data) { if (err) { res.writeHead(500, { Content-Type: text/plain }); res.end(err.message); } else { res.writeHead(200, { Content-Type: text/plain }); res.end(data); } });}); Server can deal with other requests while waiting for fileserver.listen(3000);console.log(Server listening on 3000);
    50. 50. Node libraries are structured as CommonJS moduleshttp://www.commonjs.org/specs/modules/1.0/
    51. 51. Libraries are evaluated in their own contextOnly way to share API is to attach to exports
    52. 52. require function returns exports
    53. 53. DemoWriting custom modules
    54. 54. /* people.js */function Person(name) { this.name = name;}Person.prototype = { sayHi: function() { console.log("Hi, Im " + this.name); }} Attaching API to exportsexports.createPerson = function(name) { return new Person(name);};
    55. 55. exports from module are returnedvar people = require(./people); File pathvar barry = people.createPerson(Barry);barry.sayHi(); // Hi, Im Barryconsole.log( typeof Person ); // undefined Internals of module don’t exist in this context
    56. 56. Despite being a relatively new runtime,there are a massive number of open source libraries available
    57. 57. Official list alone has 718 released modules https://github.com/ry/node/wiki/modules (as of Feb 2011)
    58. 58. Modules for just about everything you could think of, •Web Application Servers •Static File Servers •Web Middleware •Database (couchdb, mongodb, mysql, postgres, sqlite, etc..) •Templating •Build & Production •Security •SMTP •TCP/IP •Web Services •Message Queues •Testing •XML •Command Line Parsers •Parser Generators •Debugging tools •Compression •Graphics •Payment Gateways •Clients for many public API’s (facebook, flickr, last.fm, twitter, etc...) •Internationalization and probably a lot for things you’ve never heard of
    59. 59. So you can just pull them down and reference them on their files. However once libraries start depending onother libraries, and we start installing quite a few, Well, we know how this ends up...
    60. 60. Messy
    61. 61. Established platforms have tools to help with this Node is no different
    62. 62. npm is a package manager fornode. You can use it toinstall and publish yournode programs. It managesdependencies and does othercool stuff. http://npmjs.org/
    63. 63. $npm install <name>
    64. 64. Module details and dependencies are expressed in CommonJS standard package.json http://wiki.commonjs.org/wiki/Packages/1.0
    65. 65. { "name" : "vows", "description" : "Asynchronous BDD & continuousintegration for node.js", "url" : "http://vowsjs.org", "keywords" : ["testing", "spec", "test", "BDD"], "author" : "Alexis Sellier <self@cloudhead.net>", "contributors" : [], "dependencies" : {"eyes": ">=0.1.6"}, "main" : "./lib/vows", "bin" : { "vows": "./bin/vows" }, "directories" : { "test": "./test" }, "version" : "0.5.6", "engines" : {"node": ">=0.2.6"}} https://github.com/cloudhead/vows/
    66. 66. Okay.So node’s a nice idea in theory and there’s enough there to be compared to other established platforms. But where does it rock?
    67. 67. Web apps are growing up
    68. 68. In my mind, there are two major challengesfacing the next generation of web applications.
    69. 69. Being real-time
    70. 70. Being everywhere
    71. 71. Most web application platforms struggle, or at least don’t much help with these challenges
    72. 72. Node.js does
    73. 73. Being everywhere
    74. 74. Although it’s nice to think about writing applications for just new versions of Chrome, Firefox and IE9. We should strive to get our applications working everywhere*. *to at least some degree
    75. 75. And I’m not just talking about
    76. 76. Browsers are everywhere
    77. 77. With traditional web applications there are two distinct sidesClient Server
    78. 78. With node.js both sides speak the samelanguage making it easier to work together
    79. 79. The server can help thebrowser fill out functionality that it’s missing
    80. 80. DemojQuery on the Server using jsdom
    81. 81. var jsdom = require(jsdom), window = jsdom.jsdom().createWindow(), document = window.document, htmlEl = document.getElementsByTagName(html)[0];jsdom.jQueryify(window, function() { var $ = window.jQuery; $(<div />).addClass(servermade).appendTo(body); $(.servermade).text(And selectors work fine!); console.log( htmlEl.outerHTML );});
    82. 82. DemoSharing code between Server and Client
    83. 83. (function(exports) { exports.createChartOptions = function(data) { var values = parseData(data); return { ... }; }})(typeof window !== undefined ? (window.demo = {}) : exports); Attaches API to Attaches API to exports in node.js window.demo in browser to be returned by require
    84. 84. Being real-time
    85. 85. HTTP RequestClient Server Response
    86. 86. Not so good for pushingClient Server Update. Erm.... Fail
    87. 87. Web SocketsProper Duplex Communication!
    88. 88. But it’s only in very recent browsersAnd due to protocol concerns it’s now disabled even in recent browsers
    89. 89. But you’ve probably noticed thatapplications have been doing this for years We’ve got some pretty sophisticated methods of emulating it, even in IE
    90. 90. WebSockets Silverlight / FlashIE HTML Document ActiveX AJAX Long Polling AJAX multipart streaming Forever IFrame JSONP Polling
    91. 91. All of those require a server holding open a connection for as long as possible.
    92. 92. Thread-per-connection web servers struggle with this
    93. 93. Node.js doesn’t even break a sweat
    94. 94. var socket = io.listen(server);socket.on(connection, function(client){ // new client is here! client.on(message, function(){ … }) client.on(disconnect, function(){ … })});
    95. 95. DemoReal-time web using socket.io
    96. 96. var socket = io.listen(server), counter = 0;socket.on(connection, function(client) { No multithreading, so no counter = counter + 1; race condition! socket.broadcast(counter); client.on(disconnect, function() { counter = counter - 1; socket.broadcast(counter); });});
    97. 97. Node.js is young, but it’s growing up fast
    98. 98. So how do you get started with node?
    99. 99. If you’re on Mac or Linux then you’re good to go http://nodejs.org/
    100. 100. If you’re on Windowsyou’re going to be a little disappointed
    101. 101. Currently the best way to get start on Windows is hosting inside a Linux VM and ssh’ing it.http://www.lazycoder.com/weblog/2010/03/18/getting-started-with-node-js-on-windows/ Proper Windows support will be coming soon. http://nodejs.org/v0.4_announcement.html
    102. 102. I think that node is super exciting and that you should start experimenting with it today
    103. 103. But even if you don’t, other WebFrameworks are watching and it will almost certainly influence them
    104. 104. http://nodejs.org/http://groups.google.com/group/nodejs #nodejs on freenode.net http://howtonode.org/https://github.com/alexyoung/nodepad
    105. 105. Questions?
    106. 106. // Thanks for listening!return;
    107. 107. Image AttributionsOld man exmouth market, Daniel2005 - http://www.flickr.com/photos/loshak/530449376/needle, Robert Parviainen - http://www.flickr.com/photos/rtv/256102280/Skeptical, Gabi in Austin - http://www.flickr.com/photos/gabrielleinaustin/2454197457/Javascript!=Woo(), Aaron Cole - http://www.flickr.com/photos/awcole72/1936225899/Day 22 - V8 Kompressor, Fred - http://www.flickr.com/photos/freeed/5379562284/The Finger, G Clement - http://www.flickr.com/photos/illumiquest/2749137895/A Messy Tangle of Wires, Adam - http://www.flickr.com/photos/mr-numb/4753899767/7-365 I am ready to pull my hair out..., Bram Cymet - http://www.flickr.com/photos/bcymet/3292063588/Epic battle, Roger Mateo Poquet - http://www.flickr.com/photos/el_momento_i_sitio_apropiados/5166623452/Worlds Tiniest, Angelina :) - http://www.flickr.com/photos/angelinawb/266143527/A Helping Hand, Jerry Wong - http://www.flickr.com/photos/wongjunhao/4285302488/[108/365] Ill-advised, Pascal - http://www.flickr.com/photos/pasukaru76/5268559005/Gymnastics Artistic, Alan Chia - http://www.flickr.com/photos/seven13avenue/2758702332/metal, Marc Brubaker - http://www.flickr.com/photos/hometownzero/136283072/Quiet, or Youll see Father Christmas again, theirhistory - http://www.flickr.com/photos/22326055@N06/3444756625/Day 099, kylesteed - http://www.flickr.com/photos/kylesteeddesign/4507065826/Spectators in the grandstand at the Royal Adelaide Show, State Library of South Australia - http://www.flickr.com/photos/state_library_south_australia/3925493310/

    ×