Rails 5 tutorial: How to create a Chat with Action Cable

David Heinemeier Hansson recorded a screencast building a chat with Action Cable in Rails 5. Here we have a summary:

gem install rails -v 5.0.0.beta1

rails new chat --skip-spring

cd chat

rails g controller rooms show

rails g model message content:text

rails db:migrate

# app/controllers/rooms_controller.rb

class RoomsController < ApplicationController
  def show
    @messages = Message.all

# app/views/rooms/show.html.erb

<h1>Chat room</h1>
<div id="messages">

  Say something:

# app/view/messages/_message.html.erb

mkdir app/views/messages
<div class="“message”">


rails g channel room speak

# app/channels/room_channel.rb

class RoomChannel  '/cable'

And on app/assets/javascripts/cable.coffee we add:

@App ||= {}
App.cable = ActionCable.createConsumer()

# app/assets/javascripts/channels/room.coffee

App.room = App.cable.subscriptions.create "RoomChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    $('#messages').append data['message']
    # Called when there's incoming data on the websocket for this channel

  speak: (message) ->
    @perform 'speak', message: message

$(document).on 'keypress', '[data-behavior~=room_speaker]', (event) ->
  if event.keyCode is 13 # return = send
    App.room.speak event.target.value
    event.target.value = ""

# app/models/message.rb

class Message < ApplicationRecord
  after_create_commit { MessageBroadcastJob.perform_later self }

And finally we create app/jobs/message_broadcast_job.rb:

rails g job MessageBroadcast
class MessageBroadcastJob < ApplicationJob
  queue_as :default

  def perform(message)
    ActionCable.server.broadcast 'room_channel', message: render_message(message)

    def render_message(message)
      ApplicationController.renderer.render(partial: 'messages/message', locals: { message: message })

The code is on GitHub: https://github.com/HectorPerez/chat-in-rails5

And the video of DHH:

Template Method Pattern

The Template Method Pattern consists of a method that is used as a default value. It is then overwritten in classes that inherit or include it:

class Bicycle
  attr_reader :size, :chain, :tire_size

  def initialize(args={})
    @size = args[:size]
    @chain = args[:chain] || default_chain
    @tire_size = args[:tire_size] || default_tire_size

  def default_chain # <- common default

This way classes that inherit from Cycle can overwrite default_chain, removing the need of super calls and achieve less coupling:

class MountainBike < Bicycle
  def default_chain

Seen in POODR.

Avoid the need for comments

As comments are not executable, they are easily out of date. They are a form of decaying documentation.

If the code inside a method needs a comment, you probably can extract the code into a new method whose name explains what you need to clarify.

Moreover, the Single Responsibility Principle says that classes and methods should have a single responsibility so when you need comments they probably have more than one responsibility.

Therefore instead of:

def gear_inches
  # tire goes around rim twice for diameter
  ratio * rim + (tire * 2)

The following is clearer and needs no comment:

def gear_inches
  ratio * diameter

def diameter
  rim + (tire * 2)

Examples from POODR and here.

Duplication is far cheaper than the wrong abstraction

Sandy Metz said that if you find yourself passing parameters and adding conditional paths through shared code, the abstraction is incorrect. Then you should go back:

  1. Re-introduce duplication by inlining the abstracted code back into every caller.
  2. Within each caller, examine the parameters that got passed in order figure out which bits of the inlined code you actually use.
  3. Delete the bits that aren’t needed for this particular caller.

Null Object Pattern


ids = ['pig', '', 'sheep']

animals = ids.map {|id| Animal.find(id)}

animals.each do |animal|
  puts animal.nil? ? 'no animal' : animal.name


animals = ids.map{ |id| GuaranteedAnimal.find(id) }
animals.each{ |animal| puts animal.name }

class GuaranteedAnimal
  def self.find(id)
    Animal.find(id) || MissingAnimal.new

class MissingAnimal
  def name
    'no animal'

Seen in the video of Sandi Metz in RailsConf 2015:


wikidata-logoWikidata is the Wikipedia project for structured knowledge. For example it knows that the capital of Spain is Madrid. The ruby gem wikidata has a basic support to retrieve it:

require 'wikidata'