Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails with Node.js - a Reese's Moment

Jerry Cheung
September 26, 2011

Rails with Node.js - a Reese's Moment

Introduction to Node.js and how to use it alongside Rails.

Jerry Cheung

September 26, 2011
Tweet

More Decks by Jerry Cheung

Other Decks in Programming

Transcript

  1. In a Nutshell • Framework for building network apps •

    First class evented I/O Monday, September 26, 11
  2. In a Nutshell • Framework for building network apps •

    First class evented I/O • Built on top of V8 Monday, September 26, 11
  3. What it ain’t • NOT a browser side Javascript library

    • NOT a full fledged webapp framework Monday, September 26, 11
  4. A Simple Example (1..3).each do |i| contents = File.read('presentation.key') puts

    "#{i}. Finished reading file" puts "#{i}. doing something important..." end Monday, September 26, 11
  5. A Simple Example 1. Finished reading file 1. doing something

    important... 2. Finished reading file 2. doing something important... 3. Finished reading file 3. doing something important... Monday, September 26, 11
  6. A Simple Example var fs = require('fs'); for (var i=1;

    i<=3; i++) { fs.readFile('presentation.key', function(err, data) { console.log(i + ". Finished reading file"); }); console.log(i + ". doing something important..."); } Monday, September 26, 11
  7. A Simple Example var fs = require('fs'); for (var i=1;

    i<=3; i++) { fs.readFile('presentation.key', function(err, data) { console.log(i + ". Finished reading file"); }); console.log(i + ". doing something important..."); } Monday, September 26, 11
  8. A Simple Example 1. doing something important... 2. doing something

    important... 3. doing something important... 4. Finished reading file 4. Finished reading file 4. Finished reading file Monday, September 26, 11
  9. Trust Callbacks • Async programming is trickier • ...But it

    doesn’t block your other code Monday, September 26, 11
  10. Go to Jail “Node jails you into this evented- style

    programming. You can’t do things in a blocking way, you can’t write slow programs.” - Ryan Dahl Monday, September 26, 11
  11. Some Real Examples • Real-time dashboards and analytics • Push

    notifications • Fast file uploads Monday, September 26, 11
  12. File Uploaders • Slow to parse the encoded upload body

    • Post-processing & background jobs Monday, September 26, 11
  13. File Uploaders require 'sinatra' # curl -F "[email protected]" http://localhost:4567/upload post

    '/upload' do # already written to params[:data][:tempfile] "" end Monday, September 26, 11
  14. Upload Benchmarks MRI Ruby 1.8.7 and 1.9.2 with Sinatra 1.2.1

    Node 0.5.0 2.66 GHz i7 2000 iterations on a 295 byte file Monday, September 26, 11
  15. Upload Benchmarks MRI Ruby 1.8.7 and 1.9.2 with Sinatra 1.2.1

    Node 0.5.0 2.66 GHz i7 2000 iterations on a 295 byte file Monday, September 26, 11
  16. Upload Benchmarks MRI Ruby 1.8.7 and 1.9.2 with Sinatra 1.2.1

    Node 0.5.0 2.66 GHz i7 2000 iterations on a 295 byte file Ruby 67 minutes 3.5 seconds Node 23.9 seconds Monday, September 26, 11
  17. Upload Benchmarks MRI Ruby 1.8.7 and 1.9.2 with Sinatra 1.2.1

    Node 0.5.0 2.66 GHz i7 2000 iterations on a 295 byte file Ruby 67 minutes 3.5 seconds Node 23.9 seconds Monday, September 26, 11
  18. Upload Benchmarks MRI Ruby 1.8.7 and 1.9.2 with Sinatra 1.2.1

    Node 0.5.0 2.66 GHz i7 2000 iterations on a 295 byte file Ruby 67 minutes 3.5 seconds Node 23.9 seconds Monday, September 26, 11
  19. Upload Benchmarks MRI Ruby 1.8.7 and 1.9.2 with Sinatra 1.2.1

    Node 0.5.0 2.66 GHz i7 2000 iterations on a 295 byte file Ruby 67 minutes 3.5 seconds Node 23.9 seconds Monday, September 26, 11
  20. No Silver Bullet • Not a replacement for Ruby •

    Use where it makes sense Monday, September 26, 11
  21. No Silver Bullet • Not a replacement for Ruby •

    Use where it makes sense • Doesn’t guarantee scalability Monday, September 26, 11
  22. Wrapup • Server side Javascript rules! • Event driven style

    of programming Monday, September 26, 11
  23. Wrapup • Server side Javascript rules! • Event driven style

    of programming • Good for specific problems Monday, September 26, 11