railsconf 2012 “streaming rest” slides

37
RESTful Streams Building realtime applications with

Upload: trinhhanh

Post on 12-Feb-2017

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: RailsConf 2012 “Streaming REST” slides

RESTful StreamsBuilding realtime applications with

Page 2: RailsConf 2012 “Streaming REST” slides

An approach to building realtime web apps

Page 3: RailsConf 2012 “Streaming REST” slides

2007: Rails 1.2

REST

Page 4: RailsConf 2012 “Streaming REST” slides

*(mind blown)

Page 5: RailsConf 2012 “Streaming REST” slides

RESTful Rails made for a clean design pattern that was easier to test, secure, and consume as an API

Page 6: RailsConf 2012 “Streaming REST” slides

Sensible, lightweight Javascript libraries like Backbone.js and Ember.js hit the ground that play nice with RESTful backends

Page 7: RailsConf 2012 “Streaming REST” slides

//Pretty simple stuff...var user = new User();user.fetch('/users/1.json');

Page 8: RailsConf 2012 “Streaming REST” slides

HTTP Long Polling// Poll every 10 seconds to keep the channel model up-to-date.setInterval(function() { user.fetch();}, 10000);*

*As seen in the Backbone documentation

Page 9: RailsConf 2012 “Streaming REST” slides

It is simple

Page 10: RailsConf 2012 “Streaming REST” slides

DB Caches

Highly optimized Rails metal

nginx cache

Pile on the caching!

Redis counter caches

Page 11: RailsConf 2012 “Streaming REST” slides

When errors happen, there are lots of them

Hello,A project in your Airbrake account has exceeded the rate limit for errors.

Project: Rails AppAccount: Long Polling ApplicationMax rate per minute: 30

Because this is more than the number of errors allowed per minute for each project on your plan, some errors are being discarded. This should not adversely affect the performance of your application.Errors for your project will be enabled again after one minute. You will only receive this email notice once a day if the project is throttled.Increase your rate limit by upgrading your account at http://polleverywhere.airbrake.io/account/edit . We offer Pro plans with a rate limit of 1,200 exceptions a minute. Contact [email protected] for more information.

Page 12: RailsConf 2012 “Streaming REST” slides

Does not work for large datasets or streams

Page 13: RailsConf 2012 “Streaming REST” slides

Rails App Maximus

For larger development teams, monolithic apps can slow things down

Page 14: RailsConf 2012 “Streaming REST” slides

Decompose app and team into smaller pieces

Rails App

JSON API

SMS AppDesktop AppMobile Web App

Page 15: RailsConf 2012 “Streaming REST” slides

...and sprinkle in some streaming

Rails App

JSON API

SMS AppDesktop AppMobile Web App

Stream

Page 16: RailsConf 2012 “Streaming REST” slides

Stream

?

Page 17: RailsConf 2012 “Streaming REST” slides

Socket.IO didn’t feel quite right

• Problems simulating a full-duplex low-latency socket when using transports other than WS

• Routing on Channels, not URIs (no “/users/:id”)

• It felt like “too much” in the wrong areas and “too little” in the right areas

Page 18: RailsConf 2012 “Streaming REST” slides

Meteor

• New to the game, looks very promising in some areas

• For our team composition, its too tightly coupled and would end up becoming monolithic

Page 19: RailsConf 2012 “Streaming REST” slides

“What problem am I really trying to solve?”

Page 20: RailsConf 2012 “Streaming REST” slides

Web apps are really great at persisting data from clients and serving it up fast, but...

Page 21: RailsConf 2012 “Streaming REST” slides

Web apps are lousy at pushing data from the

server to the client when something changes

Page 22: RailsConf 2012 “Streaming REST” slides

“All I want to do is push resources”

Rails App

Desktop App

Stream

Chart App

Page 23: RailsConf 2012 “Streaming REST” slides

Firehose.ioBuild realtime web applications

Page 24: RailsConf 2012 “Streaming REST” slides

$ gem install firehose

# Install rabbitmq

$ firehose server

How does Firehose.io work?

Page 25: RailsConf 2012 “Streaming REST” slides

URLs are the exchange,Resources are the messages

$ curl "http://127.0.0.1:7474/users/1.json"

Publish$ curl -X PUT -d "{name: ‘Fred’}" "http://127.0.0.1:7474/users/1.json"

Subscribe

Page 26: RailsConf 2012 “Streaming REST” slides

Publishing from ActiveRecord

require ‘net/http’

class User < ActiveRecord::Base after_commit do req = Net::HTTP::Put.new("/users/#{id}/firehose.json") req.body = to_json Net::HTTP.start('127.0.0.1', 7474).request(req) endend

Page 27: RailsConf 2012 “Streaming REST” slides

Subscribing from Backbone.js

// Backbone.js and Firehose.io

var user = new User({ name: "Freddy Jones});

new Firehose.Client() .uri('//users/1.json') .message(function(msg){ return user.set(JSON.parse(msg)); }).connect();

Page 28: RailsConf 2012 “Streaming REST” slides

Current implementation runs on

Thin + RabbitMQwhen 'GET' EM.next_tick do subscription = Firehose::Subscription.new(cid) subscription.subscribe path do |payload| subscription.unsubscribe env['async.callback'].call([200, {}, [payload]]) end end Firehose::Rack::AsyncResponse

when 'PUT' body = env['rack.input'].read Firehose::Publisher.new.publish(path, body) [202, {}, []]

else [501, {}, ["#{method} not supported."]]end

Page 29: RailsConf 2012 “Streaming REST” slides

Transports only include

WebSockets +HTTP long polling

Page 30: RailsConf 2012 “Streaming REST” slides

It hangs off the side so itsMinimally Invasive

Rails App

Desktop App

Firehose.io

Chart App

Page 31: RailsConf 2012 “Streaming REST” slides

Firehose.ioExperiments

Page 32: RailsConf 2012 “Streaming REST” slides

Authorization Proxy with Goliath

Rails App

Desktop App

Firehose.io

Chart App

Authorization

Page 33: RailsConf 2012 “Streaming REST” slides

Different backendsZMQ, Redis, Erlang, node.js

Page 34: RailsConf 2012 “Streaming REST” slides

You can help!

Page 35: RailsConf 2012 “Streaming REST” slides

Firehose.ioGet it now at

Page 36: RailsConf 2012 “Streaming REST” slides

Join the team at PollEv.com/jobs

Page 37: RailsConf 2012 “Streaming REST” slides

@bradgessler