Node.js and Ruby
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Node.js and Ruby

on

  • 51,096 views

 

Statistics

Views

Total Views
51,096
Views on SlideShare
44,719
Embed Views
6,377

Actions

Likes
161
Downloads
731
Comments
4

48 Embeds 6,377

http://www.maestrosdelweb.com 3874
http://nosql.mypopescu.com 1398
http://www.startplatz.de 508
http://www.slideshare.net 331
http://integradorweb.com 78
http://localhost 30
http://paper.li 17
http://www.techgig.com 17
http://mdw.diegoescares.com 16
http://feeds.feedburner.com 14
https://twimg0-a.akamaihd.net 9
http://eventsrt.heroku.com 7
https://si0.twimg.com 6
http://flavors.me 6
http://172.25.1.151 5
http://inteligenciaartificialomb.com 4
http://webcache.googleusercontent.com 4
http://www.mashme.tv 4
http://yok.to 4
http://static.slidesharecdn.com 4
http://translate.googleusercontent.com 3
http://localhost:3000 3
http://us-w1.rockmelt.com 3
http://trunkly.com 2
http://www.linkedin.com 2
http://new.test.startplatz.de 2
http://coderwall.com 2
http://a0.twimg.com 2
https://525559992-wave-opensocial.googleusercontent.com 2
http://foros.collected.info 2
http://www.perfmatters.de 1
http://www.red-geek.com.ar 1
https://www.google.com 1
https://webcache.googleusercontent.com 1
https://twitter.com 1
http://cache.baiducontent.com 1
http://192.168.2.109 1
https://926921128-wave-opensocial.googleusercontent.com 1
http://cam.oae.com 1
http://www.verious.com 1
http://s.deeeki.com 1
http://development2.yokto.tv 1
http://www.lmodules.com 1
http://storify.com 1
http://migueldev.com 1
http://www.newsblur.com 1
https://1754171312-wave-opensocial.googleusercontent.com 1
http://www.poclu.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Node.js and Ruby Presentation Transcript

  • 1. & Ruby Saturday, April 17, 2010
  • 2. @mbleigh Saturday, April 17, 2010
  • 3. Saturday, April 17, 2010
  • 4. present.ly Saturday, April 17, 2010
  • 5. What’s Node? Saturday, April 17, 2010
  • 6. Evented I/O for Javascript Saturday, April 17, 2010
  • 7. Taking JS Beyond the Browser Saturday, April 17, 2010
  • 8. Runs on Google’s V8 Saturday, April 17, 2010
  • 9. var sys = require('sys'), http = require('http'); http.createServer(function (req, res) { setTimeout(function () { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }, 2000); }).listen(8000); sys.puts('Server running at http://127.0.0.1:8000/'); Saturday, April 17, 2010
  • 10. Supported By Node • HTTP, TCP • File I/O • Redis, Mongo, SQL (DBSlayer) Saturday, April 17, 2010
  • 11. Express: Node’s Sinatra • RESTful DSL for Node webapps • Cookies, sessions, caching, etc. • expressjs.com Saturday, April 17, 2010
  • 12. 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
  • 13. Why Node? Saturday, April 17, 2010
  • 14. Generally Speedy Saturday, April 17, 2010
  • 15. > 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
  • 16. Great at Concurrency Saturday, April 17, 2010
  • 17. Asynchronous Everything Saturday, April 17, 2010
  • 18. bit.ly/nodejs-fstream function upload_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 to be saved }); }); stream.addListener('complete', function() { res.sendHeader(200, {'Content-Type': 'text/plain'}); res.sendBody('Thanks for playing!'); res.finish(); sys.puts("n=> Done"); }); } Saturday, April 17, 2010
  • 19. Javascript is great for this. Saturday, April 17, 2010
  • 20. Browser and server, together at last Saturday, April 17, 2010
  • 21. Why Ruby? Saturday, April 17, 2010
  • 22. Are you at the right conf? Saturday, April 17, 2010
  • 23. When to Node Saturday, April 17, 2010
  • 24. Real-Time Applications Saturday, April 17, 2010
  • 25. WebSockets • Persistent server connections • Part of HTML5 • True real-time for web apps • Deprecate to Flash Sockets Saturday, April 17, 2010
  • 26. Why Real-Time? • Better on your server • Better for your user • You need more buzzwords Saturday, April 17, 2010
  • 27. 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
  • 28. 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
  • 29. Hazards of a Young Tool Saturday, April 17, 2010
  • 30. node.websocket.js • Framework-like approach • Includes fun examples • Node version headaches • github.com/guille/node.websocket.js/ Saturday, April 17, 2010
  • 31. 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
  • 32. node.ws.js • Minimal Node WebSocket server • Talk to your browser clients • Javascript all the way down • Mostly for experimentation • github.com/ncr/node.ws.js Saturday, April 17, 2010
  • 33. How can we actually use it? Saturday, April 17, 2010
  • 34. Redis is the Bridge • Super-fast, super-awesome • PUBSUB = WIN! • Swiss-army knife for your app. Saturday, April 17, 2010
  • 35. Example • Twitter-esque status streams • Want to update web interface in real time • Rails, Node, Redis, and Chrome Saturday, April 17, 2010
  • 36. The Ruby Code Saturday, April 17, 2010
  • 37. class User < ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :lockable and :timeoutable devise :database_authenticatable, :registerable, :rememberable, :validatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation def follow(other_user) Red.sadd "user:#{other_user.id}:followers", self.id Red.sadd "user:#{self.id}:follows", other_user.id end def follower_ids Red.smembers "user:#{self.id}:followers" end def follow_ids Red.smembers "user:#{self.id}:follows" end def update(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 end Saturday, April 17, 2010
  • 38. def update(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
  • 39. The Node Code Saturday, April 17, 2010
  • 40. 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
  • 41. 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
  • 42. 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
  • 43. The Browser Code Saturday, April 17, 2010
  • 44. if ("WebSocket" in window) { var ws = new WebSocket("ws://127.0.0.1:8080/timeline/" + current_user); ws.onmessage = function(evt) { $('ul.timeline').prepend("<li>" + evt.data + "</li>"); } } Saturday, April 17, 2010
  • 45. How else can we use Node? Saturday, April 17, 2010
  • 46. Asynchronous Applications Saturday, April 17, 2010
  • 47. Push APIs • Want to notify API subscribers in real-time • Utilize Webhooks • Example: GitHub’s Service Hooks Saturday, April 17, 2010
  • 48. Saturday, April 17, 2010
  • 49. File Transcoding Saturday, April 17, 2010
  • 50. Saturday, April 17, 2010
  • 51. Online Gaming Saturday, April 17, 2010
  • 52. Ephemeral Peer-to-Peer Saturday, April 17, 2010
  • 53. Wrapping up... Saturday, April 17, 2010
  • 54. Tip of the Iceberg • Node’s libraries and use cases are expanding rapidly • Async means thinking differently • Still at the early stages Saturday, April 17, 2010
  • 55. howtonode.org Saturday, April 17, 2010
  • 56. Node for Ruby? Saturday, April 17, 2010
  • 57. EventMachine Saturday, April 17, 2010
  • 58. Cramp Saturday, April 17, 2010
  • 59. Use what feels right. Saturday, April 17, 2010
  • 60. I don’t know the half of it. Saturday, April 17, 2010
  • 61. Questions? Saturday, April 17, 2010