Websocket on railsBuilding realtime webapps
Im a JavaDeveloperwho likes toride therails!
The future is now!
Whats so greatabout Websocket?
HTTP is half-duplex
Asynchronous full-duplex communicationWebsocket is asynchronous & full-duplex
So, a little less of this...
So instead of this...                        Or this...
Or this...
You tell me... whats new?
Push
So, how to ride the rails with this?
Introducing websocket-rails1.   echo "gem thin" >> Gemfile2.   echo "gem websocket-rails" >> Gemfile3.   bundle install4. ...
Rubyeventmachine
Map events to controller actionsWebsocketRails::EventMap.describe do # You can use this file to map incoming events to con...
Use rails-like controllersclass RsvpController < WebsocketRails::BaseController def initialize_session   # initialize appl...
Trigger and bind to events in the  client$ ->   dispatcher = new WebSocketRails(localhost:3000/websocket)  dispatcher.on_o...
Broadcastto anyoneout there,or...
...only push to whom is interested
Use complex messages and channels(1)class RsvpController < WebsocketRails::BaseController ... def rsvp   @rsvp_yes_count +...
Use complex messages and channels  (2)$ ->   dispatcher = new WebSocketRails(localhost:3000/websocket)  $(#rsvp_yes).bind ...
Demo
ReferencesWebsocket spec● http://dev.w3.org/html5/websockets/Websocket-rails project page● http://danknox.github.com/webso...
Websocket on Rails
Websocket on Rails
Websocket on Rails
Upcoming SlideShare
Loading in …5
×

Websocket on Rails

10,348 views

Published on

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

Published in: Technology, Education
1 Comment
11 Likes
Statistics
Notes
  • I'm new to rails, but I'm trying to learn about websockets and how to use them in rails. When I run rails s to start up my rails app, how can I tell it to use websockets instead of whatever the heck it does? That part is confusing to me. Is it just wiring up classes with the websocket?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
10,348
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
32
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide

Websocket on Rails

  1. 1. Websocket on railsBuilding realtime webapps
  2. 2. Im a JavaDeveloperwho likes toride therails!
  3. 3. The future is now!
  4. 4. Whats so greatabout Websocket?
  5. 5. HTTP is half-duplex
  6. 6. Asynchronous full-duplex communicationWebsocket is asynchronous & full-duplex
  7. 7. So, a little less of this...
  8. 8. So instead of this... Or this...
  9. 9. Or this...
  10. 10. You tell me... whats new?
  11. 11. Push
  12. 12. So, how to ride the rails with this?
  13. 13. Introducing websocket-rails1. echo "gem thin" >> Gemfile2. echo "gem websocket-rails" >> Gemfile3. bundle install4. rails g websocket_rails:install
  14. 14. Rubyeventmachine
  15. 15. Map events to controller actionsWebsocketRails::EventMap.describe do # You can use this file to map incoming events to controlleractions. # One event can be mapped to any number of controlleractions. The # actions will be executed in the order they were subscribed. namespace :rsvp do subscribe :new, :to => RsvpController, :with_method => :rsvp endend
  16. 16. Use rails-like controllersclass 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
  17. 17. 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
  18. 18. Broadcastto anyoneout there,or...
  19. 19. ...only push to whom is interested
  20. 20. 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
  21. 21. 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
  22. 22. Demo
  23. 23. ReferencesWebsocket spec● http://dev.w3.org/html5/websockets/Websocket-rails project page● http://danknox.github.com/websocket-rails/My project● https://github.com/jeroenr/followupRuby eventmachine● http://rubyeventmachine.com/

×