Node.js for Rubists


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Node.js for Rubists

  1. 1. Speaker: Sagiv Ofek
  2. 2. why?Nodes goal is to provide an easy way to buildscalable network programs.--Ryan Dahl
  3. 3. how?Keep slow operations from blocking otheroperations.
  4. 4. Traditional I/Ovar pickUpLine =; //zzzzzzztryYourBestWithThat(pickUpLine);can you tell wats wrong here?
  5. 5. ruby blocking sampleparties = Party.where(:line => [Free Drinks,Supermodels,Koby Peretz]).allparties.go
  6. 6. Modern Computer Latency● L1: 3 cycles Non - Blocking● L2: 14 cycles● RAM: 250 cycles—————————● DISK: 41,000,000 cycles Blocking● NETWORK: 240,000,000 cycles
  7. 7. Modern Computer Latency● reaching RAM is like going from here to the Red Light District.● Accessing the network is like going to the, what shall we do?
  8. 8. we have processes!time to use our gizillions cores!● does not scale well, hundreds of connections means hundreds of processes.● Spinning up a new process takes significantly more memory, a megabyte on some platforms.
  9. 9. we have threads!● Coarse-grained locks – more blocking● Fine-grained locks – more complexity● Risk for deadlocks● Hard to reason about and therefore to get right● Context switching overhead
  10. 10. we have libraries!There are asynchronous I/O libraries for Rubyas well, like EventMachine or Cool.IO forexample.And of course theres Twisted for Python,Async, Event and EV for Perl, Rx for .NET,Async Computation Expressions for F# andlibaio, libevent and libev for C. (BTW: Node.js isactually implemented using libev)
  11. 11. we have libraries!When you are within the event loop, you cannotmake any blocking calls. However, pretty muchthe entire Ruby IO, File and Dir classes, as wellas the networking and database libraries andso on are all mostly synchronous and blocking.(And the same is true for pretty much all of theother languages as well.) So, when you writecode in EventMachine or Cool.IO or Twisted,you have to be very careful which methods youcall and which libraries you use in order toavoid accidentally making blocking I/O calls.
  12. 12. nginx vs. apache● Apache uses 1 thread per connection. ○ 1 thread per connection is limiting for massive concurrency● Nginx doesnt use threads ○ it runs an event loop ○ small memory allocation per connection ○ Especially relevant when dealing with I/O All code runs in a single thread ○ No need for multithreading – no locks!
  13. 13. nginx vs. apache
  14. 14. nginx vs. apache
  15. 15. benchmarking - req/sec100 concurrent clients1 megabyte response● node 822● nginx 708● thin 85● mongrel 4
  16. 16. why should i care?● I love Ruby● I can use EventMachine for async● And, after all, my Rails app doesnt have youporn’s traffic
  17. 17. what do we want?● closures● event-driven language (callbacks)● easy to code● non-blocking, no processes/threads hassle● widely use window.onload = function() { alert("Apollo 11 landed!") }d
  18. 18. ● Ruby is a programming language and Rails is a web application framework.● Node.js is neither a language nor an application framework, its an asynchronous I/O library.● ECMAScript is crap- cant read files, load scripts, access the network. You cant even access the friggin web! (which is kind of ironic for a web scripting language) => Node is great!
  19. 19. ● A set of bindings to Google’s V8 Javascript VM (fast, super-duper fast)● A purely evented, non-blocking infrastructure that makes it super simple to build highly concurrent programs● Ability to handle thousands of concurrent connections with minimal overhead on a single process● all blocking I/O written from scratch (in C)● CommonJS module format● one code to rule them all!
  20. 20. node is● only exposes non-blocking asynchronous interfaces● only one thread, one stack (like browser)● low level features: fs, tcp, udp● has killer HTTP support
  21. 21. Async I/, function(pickUpLine) { tryYourBestWithThat(pickUpLine);});flirtWithTheUglyOne(); win!
  22. 22. installing node.js> brew install node.jsor> git clone && cdnode> ./configure && make && make install
  23. 23. time to code!demo - node console, global, process, version,env, pid, hello world
  24. 24. CommonJS & ModulesCommonJS is a community driven effort tostandardize packaging of JavaScriptlibraries, known as modules.Modules written which comply to thisstandard provide portability between othercompliant frameworks such as narwhal, andin some cases even browsers.
  25. 25. CommonJS & = function() { return Hello World;};app.jsvar hello = require(./hello);var sys = require(sys);sys.puts(;
  26. 26. http servervar sys = require(sys), http = require(http);http.createServer(function (req, res) { res.writeHead(200, {Content-Type:text/plain}); res.write(Hello World); res.end();}).listen(8000);sys.puts(Running at;
  27. 27. tcp servervar tcp = require(net);tcp.createServer(function(socket) { socket.addListener(connect, function() { socket.send("Hi, How Are You?n> "); }); socket.addListener(receive, function(data) { socket.send(data); });}).listen(4000);
  28. 28. child processvar spawn = require(child_process).spawn, ls = spawn(ls);ls.stdout.on(data, function (data) { console.log(stdout: + data);});console.log(Spawned child pid: +;ls.stdin.end();
  29. 29. libraries● Connect (middleware)● Express (sinatra)● Mongoose (db)● Expresso / should.js (TDD)● node_redis (guess)● npm● much (much) more...
  30. 30. ruby is so yesterdays news...● Tower.js● RailwayJS
  31. 31. RailwayJS● full stack MVC framework.● Doing to Node what Rails has done to Ruby.> sudo npm install railway -g> railway init blog && cd blog> npm install -l> railway generate crud post title content> railway server 8888> open
  32. 32. hello world!TEXT_IO.PUT_LINE ("Hello, World!"); adaResponse.Write("Hello, World!") asp10 PRINT "Hello, World!" basicSystem.Console.WriteLine("Hello, World!"); c#std::cout << "Hello, World!n"; c++DISPLAY "Hello, World!". cobolfmt.Printf("Hello, World!n"); goSystem.out.println("Hello, World!"); java(format t "Hello World!~%") lispprintf( "Hello, World!n" ); objectiveCprint "Hello, World!n"; perlecho Hello, World!; PHP:- write(Hello world),nl. prologputs "Hello, World!" ruby
  33. 33. Javascript ftw!one language to rule them alll
  34. 34. alert(the end!);
  1. A particular slide catching your eye?

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