Using Rails 5 ActionCable and RethinkDB to build a Reactive WebSocket App

In this article we will demonstrate how to build a WebSockets driven application using Rails 5’s ActionCable. By using RethinkDB 's active changefeeds feature we can eliminate the need for a separate event broadcasting service like Redis, resulting in elegant and succinct code.

The application that we will be building is a collaborative spreadsheet application much like Google Sheets. It will broadcast to each user the selected cells in different colors, the data in the cells and will guard against multiple users editing the same cell.

First we'll give a short introduction into how ActionCable and RethinkDB work, then we will move to the implementation of a small but powerful application. You can check out the source code of the end result by going here .

Setting up a basic ActionCable channel

ActionCable is a new Rails 5 feature that allows developers to add WebSocket services into their applications in a simple way. GoRails has an excellent full tutorial on ActionCable located here . For this article, just the bare essentials are needed.

Then, to show a page where we can make the WebSocket connection we generate a simple controller:

rails g controller spreadsheet index

Finally we set the spreadsheet index as the root route by adding the following line to the end of the draw block in config/routes.rb :

root 'spreadsheet#index'

This sequence of steps has provided us with a fully functioning WebSockets server. We can test it by running passenger start or rails s , then pointing a browser to http://localhost:3000 , opening the inspector and inspecting the /cable WebSocket connection. You should see the initial subscription to the active_users channel pass by as well as a steady stream of heartbeat frames.

Introducing RethinkDB to the application

RethinkDB is a document oriented data store much like MongoDB, but with some key advantages. It has a query language that maps very nicely to dynamic languages like Ruby, Node.JS and Python, it also has automatic sharding and replication features which makes it a relatively safe choice for applications that might scale to large numbers of users. However, the differentiating feature we are interested in for this application is changefeeds .

Changefeed queries start out like any regular query, but instead of the query going over the collection once and then returning its results the query remains alive. While the query is alive it returns any new results as they are entered into the database.

We will show how we can use changefeeds to power a reactive architecture for our web application in the next sections.

As a prerequisite, first install RethinkDB by following one of the installation guides for your operating system. Then open a new terminal and run rethinkdb to start the database system. You can visit http://localhost:8080 to browse around its administration interface.

Adding RethinkDB models to the application

The easiest way of adding RethinkDB models to your Ruby on Rails application is by using the NoBrainer gem. It will take care of connections, and wrap your documents in objects with relations and attributes just like ActiveRecord does.

To use it simply add it to your Gemfile :

gem 'nobrainer'
gem 'nobrainer_streams'

Then run bundle and rails g nobrainer:install to have your application use NoBrainer instead of ActiveRecord.

NoBrainer creates databases and tables for you automatically, so we can skip the migration writing step and move on directly to the creation of a model. Simply add the following code to app/models/user.rb :

class User
include NoBrainer::Document
field :selected_cell
end

Now we can use it in our channel by populating the lifecycle hooks and adding an action. Edit the app/channels/active_users.rb file to look like so:

The subscribed lifecycle callback now creates a new user every time a new channel is established. The stream_from invocation runs a RethinkDB query that is streamed directly to the WebSocket client. It streams all existing users, and then continues to stream users whenever they are created or destroyed.

The unsubscribed callback simply destroys the user associated with the channel. Note that this is not a very robust way of managing sessions, as the session might end without the unsubscribed hook being called in some scenarios. This results in leaking abandoned sessions in the database, but it will do for this basic demonstration.

Rendering the data on the client side

For showing the results of the query we will build a simple view by adding the following HTML to app/views/spreadsheet/index.html.erb :

To populate this view we first edit the receive method of the client side channel implementation in app/assets/javascripts/channels/active_users.coffee to update the client side model whenever new data arrives:

At this point you can open a few sessions by navigating to localhost:3000 in multiple browser tabs and see how the user list immediately updates.

Now that we have gone through the basics of setting up a basic ActionCable channel from the backend to the frontend, we will move on to make the application a little more complex and implement a multi-user spreadsheet.

Implementing a multi-user spreadsheet

From this point on we're going to show only the essence of the changes needed to make each feature work as the slightly more complex features tend to require additional logic that distracts from the core idea. If you would like to follow along and implement every step, have a look at this github repo that has every step as a single git commit.

To set the scene for our spreadsheet we add a quick and dirty spreadsheet view using jQuery and HandsOnTable . A more forward looking developer might refactor this to use React or Polymer.

We add the spreadsheet element to our HTML in app/views/spreadsheet/index.html.erb :

Streaming field selections as colorful cells

A cool feature of the multi-user spreadsheet is that you can see what cells other users have selected. To implement this feature we add an action that stores a user's cell selection to our channel in app/channels/active_users.rb :

That function in turns gets invoked every time a selection is made, or a selection is discarded, on the spreadsheet. Since we are already subscribed to the users these changes will immediately be streamed back to the channel, so we also add a render_selected_cells function that adds the user-<num> CSS class to selected cells. Both in app/assets/javascripts/spreadsheet.coffee :

Go ahead and open up a few tabs just to have fun looking at the colored cells moving around.

Transmitting the field values

This part will be really simple, yet it is the core of the application. First we introduce a model for the spreadsheet cells in app/models/spreadsheet_cell.rb :

class SpreadsheetCell
include NoBrainer::Document
field :location
field :value
end

Then we generate a channel (using rails g channel spread_sheet_cells ) and populate it with a stream for the cell values and an action for updating a cell in app/channels/spread_sheet_cells_channel.rb :

Now the only thing left to do is to implement the client side controller at app/assets/javascripts/spreadsheet.coffee . We add an afterChanged event to store the new value, and an update_cell action that sets incoming data:

Just a couple of lines and we end up with an application that is already quite impressive. Open it up in a few browser windows and add some data. The real time streaming aspect of WebSockets makes the app a really satisfying experience.

To give this application just a little bit of extra robustness we will add a slightly more advanced feature: edit locks.

Implementing locks to prevent concurrent edits

When a user starts typing in a cell it would be unfortunate if another user started editing that cell at the same time and one of their modifications was lost. The most straightforward solution to this problem is to lock the cell to whoever selects the cell for editing first.

We will implement edit protection locks as the last feature of this spreadsheet application. Starting at the backend again, first we add two methods to the User class in app/models/user.rb :

Whenever a user opens a cell for editing, before the cell allows the user to edit it will ask the server for a lock. The complex looking RethinkDB query in the lock_cell method, in one operation: looks up the cell, checks if it is locked, and if it is not locked sets the lock to the id of the user. Since we are setting the lock in the SpreadsheetCell document that all users are subscribed to, all users will receive any updates in lock statuses of cells.

The unlock_cell command looks through the entire document to find locks and releases them. We also introduced a before_destroy callback so that when the user closes their connection any locks they held are released.

On the client side we injected a monkey patch to HandsOnTable to allow us to intercept the beginEditing and finishEditing functions by setting acquireEditLock and releaseEditLock properties. They look like this:

In acquireEditLock we store the callback that enables the user to edit the cell they selected and we send the request for the lock to the active_users channel.

In releaseEditLock we simply send the unlock_cell command to the active_users channel.

Finally when in update_cell a lock is detected for the current user, we look up if there is an associated callback and invoke it.

Bleeding edge technology

Rails 5 is a fresh release and ActionCable is the new kid on the block. A consequence of working with new technologies like this is that the ecosystem is not fully adjusted to them yet. For the purposes of this blog we implemented monkey patches to both the RethinkDB Ruby driver and the NoBrainer ORM to ensure a seamless integration. They are injected through a gem but there are PR's outstanding for both the driver and the ORM . With time these or other PR's will get merged and we can enjoy these new technologies without friction.

Conclusion

After a short introduction to both the Rails 5 WebSocket component: ActionCable, and the RethinkDB database system, this article took us through the implementation of a concurrent & collaborative spreadsheet. The spreadsheet shows selected cells, guards against simultaneous edits of single cells, and immediately persists all data.

ActionCable makes setting up data channels and making remote procedure calls easy work. RethinkDB can be made to integrate with ActionCable channels and allows us to implement communication between clients that is persisted and broadcasted without the need of an additional message broker.

We hope this article has inspired you to build exciting new applications or features driven by WebSockets.

If you liked this article please stay tuned by subscribing to our mailing list, and be sure to give it an upvote on HN or Reddit. We recently published a blog post on ActionCable locatedhere that will soon be followed up with a part 2.

Phusion Passenger is open source software that ensures easy and robust deployment of your Ruby, Python, Node.js and Meteor apps. For additional reliability features and premium support, check out the enterprise edition .