-
Notifications
You must be signed in to change notification settings - Fork 22.2k
ActionCable broadcast raises ArgumentError in Ruby 3.0.0 #40993
Description
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!"}
=> nilActual 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!"}
=> nilPositional arguments and keyword arguments were separated in Ruby 3.0.0, and that is affecting ActionCable.server.broadcast when passing arguments.
rails/actioncable/lib/action_cable/server/broadcasting.rb
Lines 24 to 26 in afc79e3
| 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 :)