Setting up ActionCable - npezza93/chat GitHub Wiki
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.
-
In your Gemfile make sure to update your rails version:
gem 'rails', '>= 5.0.0'
-
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'
-
You’ll also need to use Puma as your server since it is threaded. So add it to your Gemfile.
gem 'puma'
-
bundle install
-
Create the following directory structure to handle our channels on the server side:
├── app ├── channels ├── application_cable ├── channel.rb └── connection.rb
-
channel.rb
should contain:module ApplicationCable class Channel < ActionCable::Channel::Base end end
-
connection.rb
should contain:module ApplicationCable class Channel < ActionCable::Channel::Base end end
-
Now we’ll setup the client side of ActionCable. In
app/assets/javascripts/
create acable.js
file that contains://= require action_cable //= require_self //= require_tree ./channels (function() { this.App || (this.App = {}); App.cable = ActionCable.createConsumer(); }).call(this);
-
Now add the following route to your routes file so consumers know where to connect:
mount ActionCable.server => '/cable'
-
Finally add the following meta tag helper to the head of your application.html.erb file:
<%= action_cable_meta_tag %>