Tweet about this on TwitterShare on FacebookShare on LinkedInPin on PinterestEmail this to someone
Articles

Rails 5.0.0rc1 Released – Is ActionCable Ready for Prime Time?

As the web evolves and users become more demanding, the need for web applications to be interactive and immediate grows. The engineering team at ClearVoice has been working hard to add interactive features to the ClearVoice platform to take advantage of the latest browser technology. Last month we released a major update that includes realtime messaging, presence and collaborative editing.

Before starting development of the realtime features we explored several options. We looked at building a home-grown solution with socket.io, we tested hosted services such as Pusher and PubNub, and finally we evaluated the beta of ActionCable the new realtime system built into Rails 5. At the time, ActionCable had no clear release date. Ultimately we ended up going with Pusher to solve our needs. This decision came down to the ease of implementation, cost, reliability and popularity in the community. We were not able to select ActionCable not only because it was still in beta, but also due to the lack of fallback support in the case of failed WebSocket connections. Both Pusher and PubNub have several layers of graceful fallbacks in their client implementations. They both provide fallbacks if the browser does not have native WebSocket support and will also fallback to HTTP longpolling in the case that a firewall or other network device prevents WebSockets from functioning correctly.

Although we did not select it at the time, I feel that ActionCable has a lot of potential. It makes building realtime apps with Rails incredibly straightforward. I recently gave a talk at the local Ruby::AZ Meetup group about ActionCable, including live-coding a chat system with user registrations and authentication.

Rails 5.0.0rc1

Here’s a quick peek at getting a very basic ActionCable example wired up:

First off, we need to make sure we have Ruby 2.2.2+ installed. I’m not going to include ruby install instructions here, but I recommend using rbenv, which can be easily installed via brew on Mac OSX.

As a prerequisite, check to ensure you’ve got Ruby 2.2.2+ and install rails 5.0.0rc1:

1. Create our app (the _5.0.0rc1_ tells the rails shim that we wish to use rails 5.0.0rc1), initialize a git repository and make our first commit just in case we need to revert anything we do:

2. Create a rooms controller, a message model, run the migrations to create our messages database table, commit and create a test message:

3. Okay, we’ve got the basic plumbing for a rails app with a rooms controller and a message model. Lets point the root route of the app to rooms#show. Open up config/routes.rb and replace “get ‘rooms/show'” with “root to:’rooms#show’:

4. Now it is time to fire up the app and see what we have:

Open up in the browser at http://localhost:3000 and:

5. Success! Our controller is loading. Now let’s display our messages in the room – populate the following files:

ActionCable Chat

 

<%= render @messages %>

app/views/messages/_message.html.erb:

<%= message.content %>

 

Lets commit these changes: $ git add . $ git commit -am “Add rooms/message views”

Now open up http://localhost:3000 again and see what we’ve got:

What we have here is a basic rails app with our test message rendered. This is Rails 101. Just about the simplest possible app. With ActionCable it’s just as simple to add realtime functionality, so lets do it:

6. Generate a ActionCable channel with a ‘speak’ action:

7. Implement the received and speak in app/assets/javascripts/channels/room.coffee:

#{data}

” speak: (message) -> @perform ‘speak’, message: message

8. Update the channel subscription and implement ‘speak’ in app/channels/room_channel.rb:

And lets commit:

Restart the rails server process and test in two windows at http://localhost:3000:


To see a complete chat app built including user registrations and authentication check out the video I recently made after giving a talk on the subject:

What has your experience been with the realtime web? If you’re a developer, have you checked out ActionCable? Let us know your thoughts on social media using #ClearVoice or take it to the comments on HackerNews

Join the Conversation

Subscribe to the ClearVoice newsletter
to get the best of our content delivered to your inbox.

OOPS! There were some errors in your submission. Please try again shortly.

You're in!

We heard you loud and clear. You will get a confirmation in your inbox soon.

Check Your Email Confirmation