Rails 6 Pass custom config to ActionCable::Server::Base

Taha Husain

Taha Husain

August 21, 2019

This blog is part of our  Rails 6 series.

Before Rails 6, Action Cable server used default configuration on boot up, unless custom configuration is provided explicitly.

Custom configuration can be mentioned in either config/cable.yml or config/application.rb as shown below.

# config/cable.yml

production:
  url: redis://redis.example.com:6379
  adapter: redis
  channel_prefix: custom_

Or

# config/application.rb

config.action_cable.cable = { adapter: "redis", channel_prefix: "custom_" }

In some cases, we need another Action Cable server running separately from application with a different set of configuration.

Problem is that both approaches mentioned earlier set Action Cable server configuration on application boot up. This configuration can not be changed for the second server.

Rails 6 has added a provision to pass custom configuration. Rails 6 allows us to pass ActionCable::Server::Configuration object as an option when initializing a new Action Cable server.


config = ActionCable::Server::Configuration.new
config.cable = { adapter: "redis", channel_prefix: "custom_" }

ActionCable::Server::Base.new(config: config)

For more details on Action Cable configurations, head to Action Cable docs.

Here's the relevant pull request for this change.

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.