Slide 1

Slide 1 text

Websocket on rails Building realtime webapps

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

The future is now!

Slide 5

Slide 5 text

What's so great about Websocket?

Slide 6

Slide 6 text

HTTP is half-duplex

Slide 7

Slide 7 text

Asynchronous full-duplex communication Websocket is asynchronous & full-duplex

Slide 8

Slide 8 text

So, a little less of this...

Slide 9

Slide 9 text

So instead of this... Or this...

Slide 10

Slide 10 text

Or this...

Slide 11

Slide 11 text

You tell me... what's new?

Slide 12

Slide 12 text

Push

Slide 13

Slide 13 text

So, how to ride the rails with this?

Slide 14

Slide 14 text

Introducing websocket-rails 1. echo "gem 'thin'" >> Gemfile 2. echo "gem 'websocket-rails'" >> Gemfile 3. bundle install 4. rails g websocket_rails:install

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Ruby eventmachine

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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 end end

Slide 19

Slide 19 text

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 end end

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Broadcast to anyone out there, or...

Slide 22

Slide 22 text

...only push to whom is interested

Slide 23

Slide 23 text

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 end end

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Demo

Slide 26

Slide 26 text

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/