SlideShare a Scribd company logo
Websocket on rails
Building realtime webapps
Websocket on Rails
I'm a Java
Developer
who likes to
ride the
rails!
The future is now!
What's so great
about Websocket?
HTTP is half-duplex
Asynchronous full-duplex
 communication
Websocket is asynchronous & full-duplex
So, a little less of this...
So instead of this...




                        Or this...
Or this...
You tell me... what's new?
Push
So, how to ride the rails with this?
Introducing websocket-rails
1.   echo "gem 'thin'" >> Gemfile
2.   echo "gem 'websocket-rails'" >> Gemfile
3.   bundle install
4.   rails g websocket_rails:install
Websocket on Rails
Ruby
eventmachine
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
  end
end
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
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
Broadcast
to anyone
out there,
or...
...only push to whom is interested
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
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
Demo
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/

More Related Content

PDF
Finagle - an intro to rpc & a sync programming in jvm
PPTX
From Web Developer to Hardware Developer
PDF
Backpressure in akka-streams
PPTX
Capistrano - automate all the things
PPTX
Perl: Coro asynchronous
PDF
New feature of async fakeAsync test in angular
PDF
Efficiently exposing services on Kubernetes (part 2)
PPTX
Getting Started with Capistrano
Finagle - an intro to rpc & a sync programming in jvm
From Web Developer to Hardware Developer
Backpressure in akka-streams
Capistrano - automate all the things
Perl: Coro asynchronous
New feature of async fakeAsync test in angular
Efficiently exposing services on Kubernetes (part 2)
Getting Started with Capistrano

What's hot (20)

PDF
Lies, Damn Lies, and Benchmarks
PPT
Capistrano
PDF
Any event intro
PPTX
Zeromq - Pycon India 2013
PPTX
Supplemental explanation WebSocket Demo.
PDF
Intelligent infrastructure with SaltStack
PPTX
Mule esb db_2
ODP
FOSDEM 2012: Practical implementation of promise theory in CFEngine
PPTX
Mule esb db_1
PDF
Orchestrate Event-Driven Infrastructure with SaltStack
PPTX
Demanding scripting
KEY
GPerf Using Jesque
PPTX
DJUGL - Django and AWS Lambda
PPTX
Sync with async
PPTX
Groovy example in mule
PDF
Background processing with Resque
KEY
Cramp websockets
PDF
ActionHeroJS Talk
PPTX
Caching and invalidating with managed store
PPTX
04 12-2018-scp ubnetdef-to_blue_tmp
Lies, Damn Lies, and Benchmarks
Capistrano
Any event intro
Zeromq - Pycon India 2013
Supplemental explanation WebSocket Demo.
Intelligent infrastructure with SaltStack
Mule esb db_2
FOSDEM 2012: Practical implementation of promise theory in CFEngine
Mule esb db_1
Orchestrate Event-Driven Infrastructure with SaltStack
Demanding scripting
GPerf Using Jesque
DJUGL - Django and AWS Lambda
Sync with async
Groovy example in mule
Background processing with Resque
Cramp websockets
ActionHeroJS Talk
Caching and invalidating with managed store
04 12-2018-scp ubnetdef-to_blue_tmp
Ad

Similar to Websocket on Rails (20)

PDF
Sockets and rails
PDF
Timeless - Websocket on Rails
PPTX
Pune Ruby Meetup - November 2015
PDF
Websockets, Ruby y Pusher Webprendedor 2010
PPT
ruby + websocket + haproxy
PDF
Let's Get Real (time): Server-Sent Events, WebSockets and WebRTC for the soul
PDF
WebSocket For Web Rubyists
PDF
Websockets talk at Rubyconf Uruguay 2010
PDF
Getting Started with WebSocket and Server-Sent Events using Java - Arun Gupta...
PDF
Getting Started with WebSockets and Server-Sent Events
PPTX
Fight empire-html5
PDF
WebSocket Push Fallback - Transcript.pdf
PPTX
Real time websites and mobile apps with SignalR
PDF
Learning RSocket Using RSC
PPTX
Realtime web experience with signal r
PDF
An In-Depth Comparison of WebSocket and SignalR: Pros, Cons, and Use Cases
PDF
Asynchronous Ruby
PPTX
Enhancing Mobile User Experience with WebSocket
PPTX
vlavrynovych - WebSockets Presentation
PPTX
Training Webinar: Enterprise application performance with server push technol...
Sockets and rails
Timeless - Websocket on Rails
Pune Ruby Meetup - November 2015
Websockets, Ruby y Pusher Webprendedor 2010
ruby + websocket + haproxy
Let's Get Real (time): Server-Sent Events, WebSockets and WebRTC for the soul
WebSocket For Web Rubyists
Websockets talk at Rubyconf Uruguay 2010
Getting Started with WebSocket and Server-Sent Events using Java - Arun Gupta...
Getting Started with WebSockets and Server-Sent Events
Fight empire-html5
WebSocket Push Fallback - Transcript.pdf
Real time websites and mobile apps with SignalR
Learning RSocket Using RSC
Realtime web experience with signal r
An In-Depth Comparison of WebSocket and SignalR: Pros, Cons, and Use Cases
Asynchronous Ruby
Enhancing Mobile User Experience with WebSocket
vlavrynovych - WebSockets Presentation
Training Webinar: Enterprise application performance with server push technol...
Ad

More from Jeroen Rosenberg (8)

PDF
Cooking your Ravioli "al dente" with Hexagonal Architecture
PDF
Apache Solr lessons learned
PDF
CoffeeScript
PDF
Provisioning with Vagrant & Puppet
KEY
Spring AOP
PDF
Dynamic System Configuration using SOA
PDF
Dynamic Lighting with OpenGL
PPSX
Git the fast version control system
Cooking your Ravioli "al dente" with Hexagonal Architecture
Apache Solr lessons learned
CoffeeScript
Provisioning with Vagrant & Puppet
Spring AOP
Dynamic System Configuration using SOA
Dynamic Lighting with OpenGL
Git the fast version control system

Recently uploaded (20)

PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation theory and applications.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Review of recent advances in non-invasive hemoglobin estimation
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation theory and applications.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Advanced methodologies resolving dimensionality complications for autism neur...

Websocket on Rails