ruby-on-railsactioncable

Is it possible to give Rails' ApplicationCable module a custom name?


Works:

# /channels/application_cable/connection.rb
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user
  end
end

Throws no method error in inheriting channel that "current_user" is undefined:

# /channels/custom_cable/connection.rb
module CustomCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user
  end
end


Solution

  • You can organize your custom namespace (connection and parent channel) such way:

    # app/channels/my_custom_namespace/channel.rb
    module MyCustomNamespace
      class Channel < ActionCable::Channel::Base
        # class body
      end
    end
    
    # app/channels/my_custom_namespace/connection.rb
    module MyCustomNamespace
      class Connection < ActionCable::Connection::Base
        # class body
      end
    end
    

    And change connection class:

    # config/application.rb
    # other configs
    config.action_cable.connection_class = -> { MyCustomNamespace::Connection }
    # other configs
    

    After that you can create children channels