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

Use MessageBus instead of ActionCable

Use MessageBus instead of ActionCable

JoseLuis Torres

November 15, 2016
Tweet

More Decks by JoseLuis Torres

Other Decks in Technology

Transcript

  1. Context ❖ While a lot of web apps are expected

    to be notifying the user for an asynchronous action, it was not clear specially with Rails apps what should be used in order to update the UI; execute a second, third or four action as consequence of a third party API or service. ❖ This approach started as part of refactoring to an existing approach, where we used a loop to check if there was a new update from the backend using a long polling technique to see if there was a new record or a new value.
  2. Disclaimer • This is how we approach our problem •

    It might not be the best possible approach • It worked for a current workflow • Our customers uses the app from laptops/tablets
  3. Example of Workflow The user changes a budget The backend

    updates a record The backend creates a SM BG Job Process new raster Process Enterprise Statistics Process Multiyear scenarios Notify the front-end Notify the front-end Notify the front-end
  4. Action Cable ❖ Uses web sockets ❖ Needs to be

    deployed with a special configuration with multi-threaded web servers ❖ Uses Pub/Sub ❖ Channels ❖ Consumers ❖ Stream ❖ Subscriptions
  5. Polling/Long Polling ❖ A technique to use an AJAX resource

    to check if something is new in the backend that will tell the front-end that it needs to be updated. ❖ Polling sets a loop with an interval of time and checks if there’s something new. ❖ Long Polling sends a requests to the backend and it comes back until there’s a response available.
  6. ServerSent Events (SSE) ❖ It’s a method to communicate the

    server to the client in only one-way. ❖ It is supported for most of the browsers ❖ It was supported by Rails since 4.0 ❖ Persistent connection ❖ No internet Explorer
  7. WebSockets ❖ Stateful connection ❖ Connection needs to be maintained,

    constant ❖ No need to use sessions/cookies ❖ More efficient protocol ❖ Full duplex ❖ Compatible with all browsers
  8. Action Cable ❖ It uses it’s own server process. ❖

    Uses Redis as pub/sub reference to keep track of what was broadcasted ❖ Need to use cookies to identify the current_user ❖ It needs more configuration
  9. What did we do? ❖ We found a gem that

    can be used without a lot of hassle to communicate between the backend and the browser, and the BG jobs and the browser. ❖ It works with long polling and polling, it supports multiple clients to the same channel. ❖ Used and tested with Discourse
  10. MessageBus ❖ A well maintained gem ❖ Easy to use

    ❖ Great for our purpose ❖ Can be used server to server ❖ Can be used server to client ❖ It uses redis ❖ It uses polling/long polling ❖ Supports all the browsers ❖ No need to have a separate instance
  11. Sidekiq Rails App Front End AWS:SQS Long Term Ruby script

    MessageBus Client/ReactJS/Redux New Stack BG Jobs With AASM SK workers Processors State Machine that completes actions and communicate the changes to the client with MessageBus
  12. Conclusion ❖ MessageBus is a good alternative for projects where

    an action needs to be performed outside of the web app: ❖ Outside of controllers, models, BG jobs ❖ No need to use another server process ❖ It can be used to broadcast or send messages to user ❖ It’s rock solid