Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionCable broadcast raises ArgumentError in Ruby 3.0.0 #40993

Closed
alea12 opened this issue Jan 1, 2021 · 1 comment
Closed

ActionCable broadcast raises ArgumentError in Ruby 3.0.0 #40993

alea12 opened this issue Jan 1, 2021 · 1 comment

Comments

@alea12
Copy link
Contributor

alea12 commented Jan 1, 2021

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants