Setting up ActionCable - npezza93/chat GitHub Wiki

Setting up ActionCable

NOTE: If you generated your app using Rails 5 most of the following will be setup. Some of this info was gathered from here where you can read about how to setup redis and deployment.

  1. In your Gemfile make sure to update your rails version:

    gem 'rails', '>= 5.0.0'
  2. You’ll need to either uncomment or add Redis to your Gemfile. Redis is used as a data store for transient data, syncing content across instances of your application.

    gem 'redis', '~> 3.0'
  3. You’ll also need to use Puma as your server since it is threaded. So add it to your Gemfile.

    gem 'puma'
  4. bundle install

  5. Create the following directory structure to handle our channels on the server side:

    ├── app
         ├── channels
             ├── application_cable
                 ├── channel.rb
                 └── connection.rb
  6. channel.rb should contain:

    module ApplicationCable
      class Channel < ActionCable::Channel::Base
      end
    end
  7. connection.rb should contain:

    module ApplicationCable
      class Channel < ActionCable::Channel::Base
      end
    end
  8. Now we’ll setup the client side of ActionCable. In app/assets/javascripts/ create a cable.js file that contains:

    //= require action_cable
    //= require_self
    //= require_tree ./channels
    
    (function() {
         this.App || (this.App = {});
    
         App.cable = ActionCable.createConsumer();
    }).call(this);
  9. Now add the following route to your routes file so consumers know where to connect:

    mount ActionCable.server => '/cable'
  10. Finally add the following meta tag helper to the head of your application.html.erb file:

    <%= action_cable_meta_tag %>
⚠️ **GitHub.com Fallback** ⚠️