Skip to content

Commit

Permalink
Allow passing custom config to ActionCable::Server::Base
Browse files Browse the repository at this point in the history
That allows us to create a separate, isolated Action Cable server
instance within the same app.
  • Loading branch information
palkan committed Feb 12, 2019
1 parent 7432e25 commit 3cd69fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
23 changes: 23 additions & 0 deletions actioncable/CHANGELOG.md
@@ -1,3 +1,26 @@
* Allow passing custom configuration to `ActionCable::Server::Base`.

You can now create a standalone Action Cable server with a custom configuration
(e.g. to run it in isolation from the default one):

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

CUSTOM_CABLE = ActionCable::Server::Base.new(config: config)
```

Then you can mount it in the `routes.rb` file:

```ruby
Rails.application.routes.draw do
mount CUSTOM_CABLE => "/custom_cable"
# ...
end
```

*Vladimir Dementyev*

* Add `:action_cable_connection` and `:action_cable_channel` load hooks.

You can use them to extend `ActionCable::Connection::Base` and `ActionCable::Channel::Base`
Expand Down
7 changes: 5 additions & 2 deletions actioncable/lib/action_cable/server/base.rb
Expand Up @@ -12,14 +12,17 @@ class Base
include ActionCable::Server::Broadcasting
include ActionCable::Server::Connections

cattr_accessor :config, instance_accessor: true, default: ActionCable::Server::Configuration.new
cattr_accessor :config, instance_accessor: false, default: ActionCable::Server::Configuration.new

attr_reader :config

def self.logger; config.logger; end
delegate :logger, to: :config

attr_reader :mutex

def initialize
def initialize(config: self.class.config)
@config = config
@mutex = Monitor.new
@remote_connections = @event_loop = @worker_pool = @pubsub = nil
end
Expand Down
10 changes: 1 addition & 9 deletions actioncable/test/subscription_adapter/channel_prefix.rb
Expand Up @@ -2,17 +2,9 @@

require "test_helper"

class ActionCable::Server::WithIndependentConfig < ActionCable::Server::Base
# ActionCable::Server::Base defines config as a class variable.
# Need config to be an instance variable here as we're testing 2 separate configs
def config
@config ||= ActionCable::Server::Configuration.new
end
end

module ChannelPrefixTest
def test_channel_prefix
server2 = ActionCable::Server::WithIndependentConfig.new
server2 = ActionCable::Server::Base.new(config: ActionCable::Server::Configuration.new)
server2.config.cable = alt_cable_config
server2.config.logger = Logger.new(StringIO.new).tap { |l| l.level = Logger::UNKNOWN }

Expand Down

0 comments on commit 3cd69fa

Please sign in to comment.