Skip to content

Commit

Permalink
Defer transmit calls when subscribing
Browse files Browse the repository at this point in the history
this ensures that the `transmit` calls will send data after the "confirm subscription" protocol message
  • Loading branch information
rsamoilov committed Jun 29, 2024
1 parent 3b8dddd commit 1bc1318
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/rage/cable/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,17 @@ def __register_action_proc(action_name)
""
end

is_subscribing = action_name == :subscribed
activerecord_loaded = defined?(::ActiveRecord)

method_name = class_eval <<~RUBY, __FILE__, __LINE__ + 1
def __run_#{action_name}(data)
#{if is_subscribing
<<~RUBY
@__is_subscribing = true
RUBY
end}
#{before_subscribe_chunk}
#{before_unsubscribe_chunk}
Expand Down Expand Up @@ -411,9 +418,15 @@ def broadcast(stream, data)
# transmit({ message: "Hello!" })
# end
def transmit(data)
@__connection.write(
Rage.config.cable.protocol.serialize(@__params, data)
)
message = Rage.config.cable.protocol.serialize(@__params, data)
if @__is_subscribing
# we expect a confirmation message to be sent as a result of a successful subscribe call;
# this will make sure `transmit` calls send data after the confirmation;
::Iodine.defer { @__connection.write(message) }
else
@__connection.write(message)
end
end
# Called once a client has become a subscriber of the channel.
Expand Down

0 comments on commit 1bc1318

Please sign in to comment.