Download - Node.js for Rubists
![Page 1: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/1.jpg)
Speaker: Sagiv Ofek
![Page 2: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/2.jpg)
why?
Node's goal is to provide an easy way to build scalable network programs. --Ryan Dahl
![Page 3: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/3.jpg)
how?
Keep slow operations from blocking other operations.
![Page 4: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/4.jpg)
Traditional I/O
var pickUpLine = file.read('file.txt');tryYourBestWithThat(pickUpLine);
can you tell wat's wrong here?
//zzzzzzz
![Page 5: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/5.jpg)
ruby blocking sample
parties = Party.where(:line => ['Free Drinks', 'Supermodels','Koby Peretz']).all
parties.go
![Page 6: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/6.jpg)
Modern Computer Latency
● L1: 3 cycles● L2: 14 cycles● RAM: 250 cycles—————————
● DISK: 41,000,000 cycles● NETWORK: 240,000,000 cycles
Blocking
Non - Blocking
![Page 7: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/7.jpg)
Modern Computer Latency
● reaching RAM is like going from here to the Red Light District.
● Accessing the network is like going to the moon.
so, what shall we do?
![Page 8: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/8.jpg)
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.
![Page 9: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/9.jpg)
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
![Page 10: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/10.jpg)
we have libraries!
There are asynchronous I/O libraries for Ruby as well, like EventMachine or Cool.IO for example. And of course there's Twisted for Python, Async, Event and EV for Perl, Rx for .NET, Async Computation Expressions for F# and libaio, libevent and libev for C. (BTW: Node.js is actually implemented using libev)
![Page 11: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/11.jpg)
we have libraries!
When you are within the event loop, you cannot make any blocking calls. However, pretty much the entire Ruby IO, File and Dir classes, as well as the networking and database libraries and so on are all mostly synchronous and blocking. (And the same is true for pretty much all of the other languages as well.) So, when you write code in EventMachine or Cool.IO or Twisted, you have to be very careful which methods you call and which libraries you use in order to avoid accidentally making blocking I/O calls.
![Page 12: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/12.jpg)
nginx vs. apache
● Apache uses 1 thread per connection.○ 1 thread per connection is limiting for massive
concurrency● Nginx doesn't 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!
![Page 13: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/13.jpg)
nginx vs. apache
![Page 14: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/14.jpg)
nginx vs. apache
http://blog.webfaction.com/a-little-holiday-present
![Page 15: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/15.jpg)
benchmarking - req/sec
100 concurrent clients1 megabyte response ● node 822 ● nginx 708● thin 85● mongrel 4
![Page 16: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/16.jpg)
why should i care?
● I love Ruby● I can use EventMachine for async● And, after all, my Rails app doesn't have
youporn’s traffic
![Page 17: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/17.jpg)
what do we want?
● closures● event-driven language (callbacks)● easy to code● non-blocking, no processes/threads hassle● widely usewindow.onload = function() { alert("Apollo 11 landed!")}d
![Page 18: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/18.jpg)
● Ruby is a programming language and Rails is a web application framework.
● Node.js is neither a language nor an application framework, it's an asynchronous I/O library.
● ECMAScript is crap- can't read files, load scripts, access the network. You can't even access the friggin' web! (which is kind of ironic for a web scripting language) => Node is great!
![Page 19: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/19.jpg)
● 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!
![Page 20: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/20.jpg)
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
![Page 21: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/21.jpg)
![Page 22: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/22.jpg)
Async I/O
file.read('file.txt', function(pickUpLine) { tryYourBestWithThat(pickUpLine);});flirtWithTheUglyOne();
win!
![Page 23: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/23.jpg)
installing node.js
> brew install node.jsor> git clone http://github.com/ry/node.git && cd node> ./configure && make && make install
![Page 24: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/24.jpg)
time to code!
demo - node console, global, process, version, env, pid, hello world
![Page 25: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/25.jpg)
CommonJS & Modules
CommonJS is a community driven effort to standardize packaging of JavaScript libraries, known as modules. Modules written which comply to this standard provide portability between other compliant frameworks such as narwhal, and in some cases even browsers.
![Page 26: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/26.jpg)
CommonJS & Modules
hello.jsexports.world = function() { return 'Hello World';};
app.jsvar hello = require('./hello');var sys = require('sys');sys.puts(hello.world());
![Page 27: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/27.jpg)
http server
var 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 http://127.0.0.1:8000/');
![Page 28: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/28.jpg)
tcp server
var 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);
![Page 29: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/29.jpg)
child process
var spawn = require('child_process').spawn, ls = spawn('ls');ls.stdout.on('data', function (data) { console.log('stdout: ' + data);});console.log('Spawned child pid: ' + ls.pid);ls.stdin.end();
![Page 30: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/30.jpg)
libraries
● Connect (middleware)● Express (sinatra)● Mongoose (db)● Expresso / should.js (TDD)● node_redis (guess)● npm● much (much) more...
![Page 31: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/31.jpg)
'ruby is so yesterday's news'...
● Tower.js● RailwayJS
![Page 32: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/32.jpg)
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 http://127.0.0.1:8888/posts
![Page 33: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/33.jpg)
hello world!TEXT_IO.PUT_LINE ("Hello, World!"); Response.Write("Hello, World!")10 PRINT "Hello, World!"System.Console.WriteLine("Hello, World!");std::cout << "Hello, World!\n";DISPLAY "Hello, World!".fmt.Printf("Hello, World!\n");System.out.println("Hello, World!");(format t "Hello World!~%")printf( "Hello, World!\n" );print "Hello, World!\n";echo 'Hello, World!';:- write('Hello world'),nl.puts "Hello, World!"
adaaspbasicc#c++cobolgojavalispobjectiveCperlPHPprologruby
![Page 34: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/34.jpg)
Javascript ftw!
one language to rule them alll
![Page 35: Node.js for Rubists](https://reader033.vdocuments.us/reader033/viewer/2022052619/5555a977d8b42a52568b483a/html5/thumbnails/35.jpg)
alert('the end!');