node.js: Javascript's in your backend

  • 16,831 views
Uploaded 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 …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • I wish you the best luck in educating people like this, and trying to make people think why we are doing something in a way instead of another. Because only change can make progress.
    Are you sure you want to
    Your message goes here
  • @JooBarbosa2 Hi João. I believe most are images from Flickr with appropriate licenses. You'll find an attribution slide at the end of the presentation with links to all sources.
    Are you sure you want to
    Your message goes here
  • Hi David,
    Very nice presentation. Where did you get all those images? I'd like to use some of them in my presentations.
    Thanks and nice work.
    Are you sure you want to
    Your message goes here
  • @caseykelso1 Hey Casey. Of course you're right. On callbacks I was more referring to having closures to easily share state between a callback and it's surrounding context, but admittedly that maybe wasn't as clear as it could have been. Real-time was meant in the colloquial web sense of pushing changes.
    Are you sure you want to
    Your message goes here
  • Hi David,

    A couple things about the presentation. It is nicely done.

    * Web apps are not real-time.
    * You can do callbacks in C, we do them all the time.

    Thanks
    Casey
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
16,831
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
120
Comments
5
Likes
20

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    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

Transcript

  • 1. node.jsJavaScript’s in your backend David Padbury http://davidpadbury.com @davidpadbury
  • 2. Programming 101
  • 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. static void Main(){ string name = GetName(); Console.WriteLine("Hi {0}", name);}static string GetName(){ return Console.ReadLine();}
  • 5. Looks pretty much the same?
  • 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. static void Main() Normal Method Call{ string name = GetName(); Console.WriteLine("Hi {0}", name);}static string GetName(){ return Console.ReadLine();} Blocks until ready
  • 8. What’s the big deal?Well, let’s think about what we do in a web server...
  • 9. public ActionResult View(int id){ var product = northwindDataContext.Get(id); return View(product);} Blocking Network Call (IO)
  • 10. Think about what we really do on a web server...
  • 11. Call Databases Grab Files Think about what we really do on a web server...Pull from caches Wait on other connections
  • 12. It’s all I/O
  • 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. Comparatively, I/O pretty much takes forever...
  • 15. And yet, we write code exactly the same.
  • 16. “we’re doing it wrong” - Ryan Dahl, node.js creatorhttp://www.yuiblog.com/blog/2010/05/20/video-dahl/
  • 17. So I can guess what you’re thinking, “What about multi-threading?”
  • 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. Threading ain’t freeContext Switching Execution Stacks take Memory
  • 20. Sure - but what else?
  • 21. An Event LoopUse a single thread - do little pieces of work, and do ‘em quickly
  • 22. With an event loop, you ask itto do something and it’ll get back to you when it’s done
  • 23. But, a single thread?
  • 24. nginx is event based (higher is better)http://blog.webfaction.com/a-little-holiday-present
  • 25. (lower is better)http://blog.webfaction.com/a-little-holiday-present
  • 26. Being event based is actually a pretty good way of writing heavily I/O bound servers
  • 27. So why don’t we?
  • 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. 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. CMost old school languages can’t do this
  • 31. *cough*Java *cough*
  • 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. 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. http://nodejs.org/
  • 35. Node.js is a set of JavaScript bindings forwriting high-performance network servers
  • 36. With the goal of making developing high- performance network servers easy
  • 37. Built on Google’s V8 js engine (so it’s super quick)
  • 38. Exposes only non-blocking I/O API’s
  • 39. Stops us writing code that behaves badly
  • 40. DemoBasic node.js
  • 41. console.log(Hello);setTimeout(function() { console.log(World);}, 2000);
  • 42. Prints Immediately $node app.js Hello Waits two seconds World $ Exits as there’s nothing left to do
  • 43. Node comes with a large set of libraries
  • 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. Libraries are ‘imported’ using the require function
  • 46. Just a function call var http = require(http), fs = require(fs);Returns object exposing the API
  • 47. DemoRequire and standard modules
  • 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. 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. Node libraries are structured as CommonJS moduleshttp://www.commonjs.org/specs/modules/1.0/
  • 51. Libraries are evaluated in their own contextOnly way to share API is to attach to exports
  • 52. require function returns exports
  • 53. DemoWriting custom modules
  • 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. 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. Despite being a relatively new runtime,there are a massive number of open source libraries available
  • 57. Official list alone has 718 released modules https://github.com/ry/node/wiki/modules (as of Feb 2011)
  • 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. 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. Messy
  • 61. Established platforms have tools to help with this Node is no different
  • 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. $npm install <name>
  • 64. Module details and dependencies are expressed in CommonJS standard package.json http://wiki.commonjs.org/wiki/Packages/1.0
  • 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. 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. Web apps are growing up
  • 68. In my mind, there are two major challengesfacing the next generation of web applications.
  • 69. Being real-time
  • 70. Being everywhere
  • 71. Most web application platforms struggle, or at least don’t much help with these challenges
  • 72. Node.js does
  • 73. Being everywhere
  • 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. And I’m not just talking about
  • 76. Browsers are everywhere
  • 77. With traditional web applications there are two distinct sidesClient Server
  • 78. With node.js both sides speak the samelanguage making it easier to work together
  • 79. The server can help thebrowser fill out functionality that it’s missing
  • 80. DemojQuery on the Server using jsdom
  • 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. DemoSharing code between Server and Client
  • 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. Being real-time
  • 85. HTTP RequestClient Server Response
  • 86. Not so good for pushingClient Server Update. Erm.... Fail
  • 87. Web SocketsProper Duplex Communication!
  • 88. But it’s only in very recent browsersAnd due to protocol concerns it’s now disabled even in recent browsers
  • 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. WebSockets Silverlight / FlashIE HTML Document ActiveX AJAX Long Polling AJAX multipart streaming Forever IFrame JSONP Polling
  • 91. All of those require a server holding open a connection for as long as possible.
  • 92. Thread-per-connection web servers struggle with this
  • 93. Node.js doesn’t even break a sweat
  • 94. var socket = io.listen(server);socket.on(connection, function(client){ // new client is here! client.on(message, function(){ … }) client.on(disconnect, function(){ … })});
  • 95. DemoReal-time web using socket.io
  • 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. Node.js is young, but it’s growing up fast
  • 98. So how do you get started with node?
  • 99. If you’re on Mac or Linux then you’re good to go http://nodejs.org/
  • 100. If you’re on Windowsyou’re going to be a little disappointed
  • 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. I think that node is super exciting and that you should start experimenting with it today
  • 103. But even if you don’t, other WebFrameworks are watching and it will almost certainly influence them
  • 104. http://nodejs.org/http://groups.google.com/group/nodejs #nodejs on freenode.net http://howtonode.org/https://github.com/alexyoung/nodepad
  • 105. Questions?
  • 106. // Thanks for listening!return;
  • 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/