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

Taha Husain

By Taha Husain

on 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.

1# config/cable.yml
2
3production:
4  url: redis://redis.example.com:6379
5  adapter: redis
6  channel_prefix: custom_
7

Or

1# config/application.rb
2
3config.action_cable.cable = { adapter: "redis", channel_prefix: "custom_" }
4

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.

1
2config = ActionCable::Server::Configuration.new
3config.cable = { adapter: "redis", channel_prefix: "custom_" }
4
5ActionCable::Server::Base.new(config: config)
6

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

Here's the relevant pull request for this change.