SlideShare a Scribd company logo
1 of 26
Download to read offline
Websocket on rails
Building realtime webapps
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
Ruby
eventmachine
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

What's hot

Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Zeromq - Pycon India 2013
Zeromq - Pycon India 2013
Srinivasan R
 
FOSDEM 2012: Practical implementation of promise theory in CFEngine
FOSDEM 2012: Practical implementation of promise theory in CFEngineFOSDEM 2012: Practical implementation of promise theory in CFEngine
FOSDEM 2012: Practical implementation of promise theory in CFEngine
dottedmag
 

What's hot (20)

Lies, Damn Lies, and Benchmarks
Lies, Damn Lies, and BenchmarksLies, Damn Lies, and Benchmarks
Lies, Damn Lies, and Benchmarks
 
Capistrano
CapistranoCapistrano
Capistrano
 
Any event intro
Any event introAny event intro
Any event intro
 
Zeromq - Pycon India 2013
Zeromq - Pycon India 2013Zeromq - Pycon India 2013
Zeromq - Pycon India 2013
 
Supplemental explanation WebSocket Demo.
Supplemental explanation WebSocket Demo.Supplemental explanation WebSocket Demo.
Supplemental explanation WebSocket Demo.
 
Intelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStackIntelligent infrastructure with SaltStack
Intelligent infrastructure with SaltStack
 
Mule esb db_2
Mule esb db_2Mule esb db_2
Mule esb db_2
 
FOSDEM 2012: Practical implementation of promise theory in CFEngine
FOSDEM 2012: Practical implementation of promise theory in CFEngineFOSDEM 2012: Practical implementation of promise theory in CFEngine
FOSDEM 2012: Practical implementation of promise theory in CFEngine
 
Mule esb db_1
Mule esb db_1Mule esb db_1
Mule esb db_1
 
Orchestrate Event-Driven Infrastructure with SaltStack
Orchestrate Event-Driven Infrastructure with SaltStackOrchestrate Event-Driven Infrastructure with SaltStack
Orchestrate Event-Driven Infrastructure with SaltStack
 
Demanding scripting
Demanding scriptingDemanding scripting
Demanding scripting
 
GPerf Using Jesque
GPerf Using JesqueGPerf Using Jesque
GPerf Using Jesque
 
DJUGL - Django and AWS Lambda
DJUGL - Django and AWS LambdaDJUGL - Django and AWS Lambda
DJUGL - Django and AWS Lambda
 
Sync with async
Sync with  asyncSync with  async
Sync with async
 
Groovy example in mule
Groovy example in muleGroovy example in mule
Groovy example in mule
 
Background processing with Resque
Background processing with ResqueBackground processing with Resque
Background processing with Resque
 
Cramp websockets
Cramp websocketsCramp websockets
Cramp websockets
 
ActionHeroJS Talk
ActionHeroJS TalkActionHeroJS Talk
ActionHeroJS Talk
 
Caching and invalidating with managed store
Caching and invalidating with managed storeCaching and invalidating with managed store
Caching and invalidating with managed store
 
04 12-2018-scp ubnetdef-to_blue_tmp
04 12-2018-scp ubnetdef-to_blue_tmp04 12-2018-scp ubnetdef-to_blue_tmp
04 12-2018-scp ubnetdef-to_blue_tmp
 

Similar to Websocket on Rails

rx.js make async programming simpler
rx.js make async programming simplerrx.js make async programming simpler
rx.js make async programming simpler
Alexander Mostovenko
 
Background Jobs - Com BackgrounDRb
Background Jobs - Com BackgrounDRbBackground Jobs - Com BackgrounDRb
Background Jobs - Com BackgrounDRb
Juan Maiz
 

Similar to Websocket on Rails (20)

Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016
 
Asynchronous Programming FTW! 2 (with AnyEvent)
Asynchronous Programming FTW! 2 (with AnyEvent)Asynchronous Programming FTW! 2 (with AnyEvent)
Asynchronous Programming FTW! 2 (with AnyEvent)
 
DevOps
DevOpsDevOps
DevOps
 
RxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixRxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMix
 
An intro to Docker, Terraform, and Amazon ECS
An intro to Docker, Terraform, and Amazon ECSAn intro to Docker, Terraform, and Amazon ECS
An intro to Docker, Terraform, and Amazon ECS
 
Introduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoaIntroduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoa
 
rx.js make async programming simpler
rx.js make async programming simplerrx.js make async programming simpler
rx.js make async programming simpler
 
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
WebCamp:Front-end Developers Day. Александр Мостовенко "Rx.js - делаем асинхр...
 
Mongo à la Resque
Mongo à la ResqueMongo à la Resque
Mongo à la Resque
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express
 
Monitoring MySQL with Prometheus and Grafana
Monitoring MySQL with Prometheus and GrafanaMonitoring MySQL with Prometheus and Grafana
Monitoring MySQL with Prometheus and Grafana
 
OSMC 2017 | Monitoring MySQL with Prometheus and Grafana by Julien Pivotto
OSMC 2017 | Monitoring  MySQL with Prometheus and Grafana by Julien PivottoOSMC 2017 | Monitoring  MySQL with Prometheus and Grafana by Julien Pivotto
OSMC 2017 | Monitoring MySQL with Prometheus and Grafana by Julien Pivotto
 
Background Jobs - Com BackgrounDRb
Background Jobs - Com BackgrounDRbBackground Jobs - Com BackgrounDRb
Background Jobs - Com BackgrounDRb
 
Small pieces loosely joined
Small pieces loosely joinedSmall pieces loosely joined
Small pieces loosely joined
 
Reusing your frontend JS on the server with V8/Rhino
Reusing your frontend JS on the server with V8/RhinoReusing your frontend JS on the server with V8/Rhino
Reusing your frontend JS on the server with V8/Rhino
 
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Managing State in React Apps with RxJS by James Wright at FrontCon 2019Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
 
Capistrano, Puppet, and Chef
Capistrano, Puppet, and ChefCapistrano, Puppet, and Chef
Capistrano, Puppet, and Chef
 
Rhive 0.0 3
Rhive 0.0 3Rhive 0.0 3
Rhive 0.0 3
 
Celery with python
Celery with pythonCelery with python
Celery with python
 

More from Jeroen Rosenberg

More from Jeroen Rosenberg (8)

Cooking your Ravioli "al dente" with Hexagonal Architecture
Cooking your Ravioli "al dente" with Hexagonal ArchitectureCooking your Ravioli "al dente" with Hexagonal Architecture
Cooking your Ravioli "al dente" with Hexagonal Architecture
 
Apache Solr lessons learned
Apache Solr lessons learnedApache Solr lessons learned
Apache Solr lessons learned
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Provisioning with Vagrant & Puppet
Provisioning with Vagrant & PuppetProvisioning with Vagrant & Puppet
Provisioning with Vagrant & Puppet
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
Dynamic System Configuration using SOA
Dynamic System Configuration using SOADynamic System Configuration using SOA
Dynamic System Configuration using SOA
 
Dynamic Lighting with OpenGL
Dynamic Lighting with OpenGLDynamic Lighting with OpenGL
Dynamic Lighting with OpenGL
 
Git the fast version control system
Git the fast version control systemGit the fast version control system
Git the fast version control system
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Websocket on Rails