Node Js and ruby

Embed Size (px)

Citation preview

  • 7/28/2019 Node Js and ruby

    1/61

    & Ruby

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    2/61

    @mbleigh

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    3/61

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    4/61

    present.ly

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    5/61

    Whats Node?

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    6/61

    Evented I/OforJavascript

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    7/61

  • 7/28/2019 Node Js and ruby

    8/61

    Runs onGoogles V8

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    9/61

    var sys = require('sys'),

    http = require('http');

    http.createServer(function (req, res) {

    setTimeout(function () {

    res.writeHead(200, {'Content-Type': 'text/plain'});

    res.end('Hello World\n');

    }, 2000);

    }).listen(8000);

    sys.puts('Server running at http://127.0.0.1:8000/');

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    10/61

    Supported By Node

    HTTP, TCP File I/O

    Redis, Mongo, SQL (DBSlayer)

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    11/61

    Express: Nodes Sinatra

    RESTful DSL for Node webapps Cookies, sessions, caching, etc.

    expressjs.com

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    12/61

    require.paths.unshift('path/to/express/lib')

    require('express')

    get('/', function(){

    this.redirect('/hello/world')

    })

    get('/hello/world', function(){

    return'Hello World'

    })

    run()

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    13/61

    Why Node?

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    14/61

    Generally Speedy

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    15/61

    > summary(node1$ttime)Min. 1st Qu. Median Mean 3rd Qu. Max.

    0.0000 0.0000 1.0000 0.7437 1.0000 106.0000

    > summary(thin1$ttime)Min. 1st Qu. Median Mean 3rd Qu. Max.0.000 1.000 1.000 1.122 1.000 74.000

    > summary(narwhal1$ttime)Min. 1st Qu. Median Mean 3rd Qu. Max.15.00 22.00 23.00 23.74 24.00 88.00

    > summary(v8cgi1$ttime)Min. 1st Qu. Median Mean 3rd Qu. Max.12.00 13.00 13.00 14.49 18.00 39.00

    four.livejournal.com/1019177.html

    Saturday, April 17, 2010

    http://four.livejournal.com/1019177.htmlhttp://four.livejournal.com/1019177.htmlhttp://four.livejournal.com/1019177.htmlhttp://four.livejournal.com/1019177.html
  • 7/28/2019 Node Js and ruby

    16/61

    Great atConcurrency

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    17/61

    AsynchronousEverything

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    18/61

    functionupload_file(req, res) {req.setBodyEncoding('binary');

    var stream = new multipart.Stream(req);stream.addListener('part', function(part) {part.addListener('body', function(chunk) {

    var progress = (stream.bytesReceived / stream.bytesTotal *100).toFixed(2);

    var mb = (stream.bytesTotal / 1024 / 1024).toFixed(1);

    sys.print("Uploading "+mb+"mb ("+progress+"%)\015");

    // chunk could be appended to a file if the uploaded file needs tobe saved

    });});stream.addListener('complete', function() {res.sendHeader(200, {'Content-Type': 'text/plain'});res.sendBody('Thanks for playing!');res.finish();sys.puts("\n=> Done");

    });}

    bit.ly/nodejs-fstream

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    19/61

    Javascript isgreat for this.

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    20/61

    Browser and server,together at last

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    21/61

    Why Ruby?

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    22/61

    Are you at theright conf?

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    23/61

    When to Node

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    24/61

    Real-TimeApplications

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    25/61

    WebSockets

    Persistent server connections Part of HTML5 True real-time for web apps Deprecate to Flash Sockets

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    26/61

    Why Real-Time?

    Better on your server Better for your user

    You need more buzzwords

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    27/61

    var socket = new WebSocket("ws://www.websocket.org");

    socket.onopen = function(evt) { alert("Open."); };

    socket.onmessage = function(evt) { alert(evt.data); };socket.onclose = function(evt) { alert("Closed."); };

    socket.send("Hello Web Socket!");

    socket.close();

    Saturday, April 17, 2010

    http://www.websocket.org/http://www.websocket.org/
  • 7/28/2019 Node Js and ruby

    28/61

    Why JS for WebSockets?

    You already write your client

    interaction code in Javascript

    Just an extension of that

    Same interface throughout

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    29/61

    Hazards of aYoung Tool

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    30/61

    node.websocket.js

    Framework-like approach Includes fun examples Node version headaches github.com/guille/node.websocket.js/

    Saturday, April 17, 2010

    http://github.com/guille/node.websocket.js/http://github.com/guille/node.websocket.js/http://github.com/guille/node.websocket.js/
  • 7/28/2019 Node Js and ruby

    31/61

    Socket.IO

    Multi-transport socket for Node Comes with client JS library For production usage github.com/rosepad/socket.io-node

    Saturday, April 17, 2010

    http://github.com/rosepad/socket.io-nodehttp://github.com/rosepad/socket.io-nodehttp://github.com/rosepad/socket.io-node
  • 7/28/2019 Node Js and ruby

    32/61

    node.ws.js

    Minimal Node WebSocket server

    Talk to your browser clientsJavascript all the way down

    Mostly for experimentation github.com/ncr/node.ws.js

    Saturday, April 17, 2010

    http://github.com/ncr/node.ws.jshttp://github.com/ncr/node.ws.jshttp://github.com/ncr/node.ws.js
  • 7/28/2019 Node Js and ruby

    33/61

    How can weactually use it?

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    34/61

    Redis is the Bridge

    Super-fast, super-awesome PUBSUB = WIN!

    Swiss-army knife for your app.

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    35/61

    Example

    Twitter-esque status streams

    Want to update web interface inreal time

    Rails, Node, Redis, and Chrome

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    36/61

    The Ruby Code

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    37/61

    classUser < ActiveRecord::Base# Include default devise modules. Others available are:# :token_authenticatable, :lockable and :timeoutabledevise :database_authenticatable, :registerable, :rememberable, :validatable

    # Setup accessible (or protected) attributes for your model

    attr_accessible:email, :password, :password_confirmation

    deffollow(other_user) Red.sadd "user:#{other_user.id}:followers", self.id Red.sadd "user:#{self.id}:follows", other_user.id end

    deffollower_ids Red.smembers "user:#{self.id}:followers" end

    deffollow_ids Red.smembers "user:#{self.id}:follows" enddefupdate(text)

    (follower_ids + [self.id]).each do |uid| Red.lpush "user:#{uid}:timeline", text Red.lpush "user:#{uid}:updates", text Red.publish "user:#{uid}:timeline", text end endend

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    38/61

    defupdate(text)

    (follower_ids + [self.id]).each do |uid|

    Red.lpush "user:#{uid}:timeline", text

    Red.lpush "user:#{uid}:updates", text

    Red.publish "user:#{uid}:timeline", text

    end

    end

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    39/61

    The Node Code

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    40/61

    var sys = require("sys"),ws = require("./ws"),redis = require("./redis-client");

    var pubsub = redis.createClient();

    pubsub.stream.addListener('connect', function() {pubsub.subscribeTo("user:*:timeline", function(channel, data) {

    var uid = channel.toString().split(':')[1];

    if (clients[uid]) {sys.debug("Writing " + data + " to " + uid)clients[uid].write(data);

    } else {

    sys.debug("User " + clients[uid] + " is not connected.");}

    });});

    ws.createServer(function (websocket) { var user_id = null; var websocket = websocket;

    websocket.addListener("connect", function (resource) {user_id = resource.match(/timeline\/([0-9]+)$/i)[1]clients[user_id] = websocket;

    }).addListener("close", function() {sys.debug("User " + user_id + " disconnected.")

    });

    }).listen(8080);

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    41/61

    ws.createServer(function (websocket) { var user_id = null;

    var websocket = websocket;

    websocket.addListener("connect", function (resource) {

    user_id = resource.match(/timeline\/([0-9]+)$/i)[1]

    clients[user_id] = websocket;

    }).addListener("close", function() {

    sys.debug("User " + user_id + " disconnected.")

    });}).listen(8080);

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    42/61

    pubsub.stream.addListener('connect', function() {pubsub.subscribeTo("user:*:timeline", function(channel, data) {

    var uid = channel.toString().split(':')[1];

    if (clients[uid]) {

    sys.debug("Writing " + data + " to " + uid)clients[uid].write(data);

    } else {

    sys.debug("User " + clients[uid] + " is not connected.");

    }

    });});

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    43/61

    The Browser Code

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    44/61

    if ("WebSocket" in window) { var ws = new WebSocket("ws://127.0.0.1:8080/timeline/" +current_user);

    ws.onmessage = function(evt) {$('ul.timeline').prepend("" + evt.data + "");

    }}

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    45/61

    How else can weuse Node?

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    46/61

    AsynchronousApplications

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    47/61

    PushAPIs

    Want to notify API subscribers inreal-time

    Utilize Webhooks

    Example: GitHubs Service Hooks

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    48/61

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    49/61

    File Transcoding

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    50/61

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    51/61

    Online Gaming

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    52/61

    EphemeralPeer-to-Peer

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    53/61

    Wrapping up...

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    54/61

    Tip of the Iceberg

    Nodes libraries and use casesare expanding rapidly

    Async means thinking differently

    Still at the early stages

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    55/61

    howtonode.org

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    56/61

    Node for Ruby?

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    57/61

    EventMachine

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    58/61

  • 7/28/2019 Node Js and ruby

    59/61

    Use what feels right.

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    60/61

    I dont knowthe half of it.

    Saturday, April 17, 2010

  • 7/28/2019 Node Js and ruby

    61/61

    Questions?