websocket on rails

26
Websocket on rails Building realtime webapps

Upload: jeroen-rosenberg

Post on 19-May-2015

8.101 views

Category:

Technology


0 download

DESCRIPTION

Short showcase how to use the awesome websocket technology in a Rails project using websocket-rails

TRANSCRIPT

Page 1: Websocket on Rails

Websocket on railsBuilding realtime webapps

Page 2: Websocket on Rails
Page 3: Websocket on Rails

I'm a Java Developer who likes to ride the rails!

Page 4: Websocket on Rails

The future is now!

Page 5: Websocket on Rails

What's so great about Websocket?

Page 6: Websocket on Rails

HTTP is half-duplex

Page 7: Websocket on Rails

Asynchronous full-duplex communicationWebsocket is asynchronous & full-duplex

Page 8: Websocket on Rails

So, a little less of this...

Page 9: Websocket on Rails

So instead of this...

Or this...

Page 10: Websocket on Rails

Or this...

Page 11: Websocket on Rails

You tell me... what's new?

Page 12: Websocket on Rails

Push

Page 13: Websocket on Rails

So, how to ride the rails with this?

Page 14: Websocket on Rails

Introducing websocket-rails

1. echo "gem 'thin'" >> Gemfile2. echo "gem 'websocket-rails'" >> Gemfile3. bundle install4. rails g websocket_rails:install

Page 15: Websocket on Rails
Page 16: Websocket on Rails

Ruby eventmachine

Page 17: Websocket on Rails
Page 18: Websocket on Rails

Map events to controller actions

WebsocketRails::EventMap.describe do # You can use this file to map incoming events to controller actions. # One event can be mapped to any number of controller actions. The # actions will be executed in the order they were subscribed. namespace :rsvp do subscribe :new, :to => RsvpController, :with_method => :rsvp endend

Page 19: Websocket on Rails

Use rails-like controllers

class RsvpController < WebsocketRails::BaseController def initialize_session # initialize application scoped variables here @rsvp_yes_count = 0 end

def rsvp @rsvp_yes_count += 1 if message broadcast_message :new_rsvp, @rsvp_yes_count endend

Page 20: Websocket on Rails

Trigger and bind to events in the client

$ -> dispatcher = new WebSocketRails('localhost:3000/websocket')

dispatcher.on_open = (data) -> console.log "Connection has been established: #{data}"

$('#rsvp_yes').bind 'click', (message) => dispatcher.trigger 'rsvp.new', true

dispatcher.bind 'new_rsvp', (rsvp_yes_count) => $('#rsvp_yes_count').html rsvp_yes_count

Page 21: Websocket on Rails

Broadcast to anyone out there, or...

Page 22: Websocket on Rails

...only push to whom is interested

Page 23: Websocket on Rails

Use complex messages and channels (1)

class RsvpController < WebsocketRails::BaseController ... def rsvp @rsvp_yes_count += 1 if message[:attending] rsvp = { :yes => @rsvp_yes_count } WebsocketRails[:rsvp].trigger 'new', rsvp endend

Page 24: Websocket on Rails

Use complex messages and channels (2)

$ -> dispatcher = new WebSocketRails('localhost:3000/websocket')

$('#rsvp_yes').bind 'click', (message) => rsvp = attending: true dispatcher.trigger 'rsvp.new', rsvp

channel = dispatcher.subscribe 'rsvp' channel.bind 'new', (rsvp) => $('#rsvp_yes_count').html rsvp.yes

Page 25: Websocket on Rails

Dem

o

Page 26: Websocket on Rails

References

Websocket spec● http://dev.w3.org/html5/websockets/

Websocket-rails project page● http://danknox.github.com/websocket-rails/

My project● https://github.com/jeroenr/followup

Ruby eventmachine● http://rubyeventmachine.com/