Anycable with rails 6.

ispirett
3 min readSep 23, 2020

Once your app is gaining traction there comes a time when you will need to switch from action cable to Any cable. This is how we got Any cable up and running on bounce.so. Bounce allows teachers and students to connect in an innovative way, you can become a teacher too. Check us out bounce.so

Running Locally

(1): Preparing your environment.

Add the following gems to your gem file.

#Gemfile_____gem "anycable-rails" --------
gem "redis", ">= 4.0"

Note: Ensure you have Redis installed and running on your platform.

(a) In your terminal for your rails app, run rails g anycable:install and follow the instructions carefully.

(b) Setup anycable-go server

The easiest way to install AnyCable-Go is to download a pre-compiled binary.

MacOS users could install it with Homebrew brew install anycable-go

(c ) Install Redis and start the service on your machine.

(2): Configuring Anycable

A couple of files would have been edited and some created after running the Any cable setup command.

(a): In your development.rb file, edit them to match the following.

"ws://localhost:8080/cable"  to "ws://localhost:3334/cable"

(b): In your application.html.rb file add between the head tag but before any javascript tags the following.

<%= action_cable_meta_tag %>

(c ): channels/application_cable/connection.rb ensure you have the following configuration.

module ApplicationCable  class Connection < ActionCable::Connection::Base
identified_by :current_user


def connect
self.current_user = find_verified_user || reject_unauthorized_connection
end

protected
def
find_verified_user
session_key = Rails.application.config.session_options[:key]
env['rack.session'] = cookies.encrypted[session_key || raise("No cookies key in config")]

if verified_user = Warden::SessionSerializer.new(env).fetch(:user)
verified_user
else
reject_unauthorized_connection
end
end
end

(d) In your config/initializers create two files anycable.rb and session_store.rb

anycable.rb AnyCable::Rails::Rack.middleware.use Warden::Manager do |config|        Devise.warden_config = config
end
session_store.rbRails.application.config.session_store :cookie_store, key: "_any_cable_session", domain: :all # or domain: '.hello.com'

(3): Anycable-go server

Anycable-Go server is a key part of our anycable setup. This is where we leverage the power of Go.

(a) Once you have already installed Anycable-Go, run the server in a separate terminal.

On mac:  anycable-go --host=localhost --port=3334
On linux ./anycable-go --host=localhost --port=3334

For Linux, if you download the precompiled binary use the above command. If you add Anycable-Go to your bash path then your command should be the mac command above.

(4): Running your app

You should now run your app with the following.

rails s
bundle exec anycable

You should now have your app server running on port 3000, your Anycable-rpc-server running on port: 50051, and your Anycable-go server running on port=3334.

Remember to ensure that your Redis server is running. By default, anycable will connect to it without any extra configuration.

Deploying to Heroku

Firstly the configuration is going to be a little more involved for Heroku. Let us start by setting up two apps.

Note: It’s best to use Heroku pipeline for this setup. https://devcenter.heroku.com/articles/pipelines

(1) Both Apps

(a) Ensure both apps have domains with Heroku free SSL enabled. This will save you a lot of headaches, for one cookie authentication for anycable will not work on .herokuapp.com domains.

For example your main app’s domain https://hello.com and for your anycable-app domain https://ws.hello.com.

(b) Both apps should use the same branch.

(c ) Both apps should use the same resources

(d ) Both apps should have these config vars.

ANYCABLE_DEPLOYMENTCABLE_URL

(e) Create or edit your Procfile. Your web: sever should be changed to this.

web: [[ "$ANYCABLE_DEPLOYMENT" == "true" ]] && bundle exec anycable --server-command="anycable-go" ||  bundle exec rails server -p $PORT -b 0.0.0.0

This configuration uses the ANYCABLE_DEPLOYMMENT VAR to configure each app specifically.

(2) Main app

(a) CONFIG VARS

ANYCABLE_DEPLOYMMENT=falseCABLE_URL= https://ws.hello.com

Cable URL could be whatever domain you like. At bounce, we created a ws subdomain for our domain.

(b) This app should have the Heroku database and Heroku Redis addons

(3) Anycable-go server app

(a) CONFIG VARS

ANYCABLE_DEPLOYMMENT=trueANYCABLE_HOST= 0.0.0.0CABLE_URL= https://ws.hello.comREDIS_URL= copy of the main app.DATABASE_URL=copy of the main app.RAILS_MASTER_KEY= same as main appSECRET_KEY_BASE= same as main app

(b) This app should have a copy of the main app’s Heroku database and Heroku Redis addons from the main app.

Testing your app

Congratulations if you made it this far, you should now have your main app linked to your Anycable-go server. Everything should be working as expected.

Troubleshooting

You can visit the Anycable docs if you run into trouble https://docs.anycable.io/#/v1/troubleshooting

Feel free to join bounce.so

Bounce: bounce twitter

LinkedIn

Github

Twitter

Dev.to

--

--

ispirett

AI & Web3 enthusiast | Web & Modbile Dev | Passionate about pushing boundaries in tech | Ready for the future |