Let’s check how we can implement RethinkDB with ActionCable into Ruby on Rails application.
You can find all code in rethinkdb_example.
First of all, let’s create sample app with rails command:
And setup ActionCable route in routes.rb:
We need to generate channel for active users:
Let’s generate controller and set it to the root in routes.rb
Now, moving to RethinkDB.
RethinkDB is a document oriented data store much like MongoDB, but with some key advantages.
RethinkDB makes building and scaling realtime apps dramatically easier. Get started by installing the server, and jump into our getting started guide to start building your first app in minutes.
You can read more here.
Let’s integrate RethinkDB into our rails application.
We need two gems.
Run bundle and installer script, to have your application use NoBrainer instead of ActiveRecord.
Now, if you generate model User with rails g model User it will look like:
Now, let’s move to our ActiveUsersChannel. Here we have subscribed and unsubscribed methods.
They’re just calling then somebody open or close connection.
Now, let’s change our tasks/index.html.erb we add here special div with active_users_list ID.
Now let’s take a look into generated active_users.coffee file:
A little change here, for now just keep received event:
Just update our ul by ID and add new data:
Now you can open browser window, then open second one, so you can see how new user will be added to the list. Or deleted - if you close browser.