Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WebSockets in Rails: ActionCable

WebSockets in Rails: ActionCable

Matic Jurglič

November 26, 2016
Tweet

More Decks by Matic Jurglič

Other Decks in Programming

Transcript

  1. WebSockets • Communication protocol, introduced in 2010 • Holding the

    connection to the server open • Bi-directional persistent connection • HTTP handshake = Upgrade request
  2. Path to Real-Time Rails • Polling • Long polling •

    WebSocket servers: Faye, Firehose,… • External hosted services: Pusher, Firebase, PubNub,…
  3. ActionCable • Rails 5 feature • Seamless integration of WebSockets

    into Rails apps • Full-stack, all ends are covered • Simple to implement and use anywhere in the application
  4. Action Cable Terminology • WebSocket connection => ActionCable::Connection • Connection

    client is called Consumer • Consumer can subscribe to multiple channels • When Consumer subscribes, they act as a subscriber • Channel is streaming broadcastings
  5. Setting up the front-end • A port of ActionCable JavaScript

    library is also available for Ember, React, Vanilla JS,…
  6. Browser (consumer) Action Cable server JavaScript Geocoder (Google maps API)

    Washington Sevnica Sevnica 14.505751. Washington 46.056946 38.889931 -77.009003
  7. Redis! • Keeps messages synced across connections and instances of

    the application • Every broadcast is sent to Redis event queue first • Can also use PG • concurrent-ruby
  8. Running the Cable • ActionCable does not need a multi-threaded

    server (works with Unicorn, Puma, Passenger) • Action Cable uses the Rack socket hijacking API to take over control of connections from the application server • Action Cable then manages connections internally, in a multithreaded manner
  9. Bad parts • Memory leaks (?) • Single point of

    failure (Redis) • Latency • No ACK for publishing from client to server • No message ordering • Scaling (https://dockyard.com/blog/2016/08/09/ phoenix-channels-vs-rails-action-cable)