Skip to content

ActionCable broadcast raises ArgumentError in Ruby 3.0.0 #40993

@alea12

Description

@alea12

ActionCable.server.broadcast raises ArgumentError in Ruby 3.0.0 when the message parameter is passed traditionally without using hashes, making the documented code in Rails Guides inoperative.

Steps to reproduce

$ rails -v
Rails 6.1.0
$ ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]
$ rails new action_cable_test && cd action_cable_test
$ rails c

In rails console:

irb(main):001:0> ActionCable.server.broadcast('my_channel', message: 'hello, world!')
Traceback (most recent call last):
        1: from (irb):1:in `<main>'
ArgumentError (wrong number of arguments (given 1, expected 2))

Expected behavior

I've confirmed the following behavior in Ruby 2.7.2:

irb(main):001:0> ActionCable.server.broadcast('my_channel', message: 'hello, world!')
[ActionCable] Broadcasting to my_channel: {:message=>"hello, world!"}
=> nil

Actual behavior

ArgumentError

System configuration

Rails version: Rails 6.1.0
Ruby version: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin20]

Workaround & The root cause

Passing the message argument as a hash is a workaround:

irb(main):001:0> ActionCable.server.broadcast('my_channel', { message: 'hello, world!' })
[ActionCable] Broadcasting to my_channel: {:message=>"hello, world!"}
=> nil

Positional arguments and keyword arguments were separated in Ruby 3.0.0, and that is affecting ActionCable.server.broadcast when passing arguments.

def broadcast(broadcasting, message, coder: ActiveSupport::JSON)
broadcaster_for(broadcasting, coder: coder).broadcast(message)
end

However, passing messages to ActionCable.server.broadcast without using the hash is common usage, which is also documented in Action Cable Overview — Ruby on Rails Guides. I think we should consider updating the guide or the source code. If we need a patch to the guide, I'd be ready to submit one :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions