Skip to content

Commit

Permalink
fix: whispers
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Sep 13, 2024
1 parent a4f1ef1 commit ed5ab22
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Adding `whisper: true` to the stream subscription enables **sending** broadcasts

**IMPORTANT:** There can be only one whisper stream per channel subscription (since from the protocol perspective clients don't know about streams).

**NOTE:** This feature is supported by Action Cable server (when `anycable-rails` is loaded), too.
**NOTE:** This feature is partially supported by Action Cable server (when `anycable-rails` is loaded). The difference is that _whispers_ are broadcasted to all clientes, including the initiator.

## Helpers

Expand Down
2 changes: 2 additions & 0 deletions etc/anyt/whisper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def subscribed
scenario %(
Only other clients receive the whisper message
) do
skip if Anyt.config.use_action_cable

perform_request = {
:command => "whisper",
:identifier => {channel: channel}.to_json,
Expand Down
9 changes: 6 additions & 3 deletions lib/anycable/rails/ext/whisper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
require "action_cable"

ActionCable::Channel::Base.prepend(Module.new do
private attr_accessor :whisper_stream
attr_accessor :whisper_stream

def stream_from(broadcasting, _callback = nil, **opts)
whispering = opts.delete(:whisper)
whispers_to(broadcasting) if whispering
super
end

def whispers_to(broadcasting) = self.whisper_stream = broadcasting
def whispers_to(broadcasting)
logger.debug "#{self.class.name} whispers to #{broadcasting}"
self.whisper_stream = broadcasting
end
end)

ActionCable::Connection::Subscriptions.prepend(Module.new do
Expand All @@ -26,6 +29,6 @@ def whisper(data)
stream = subscription.whisper_stream
raise "Whispering stream is not set" unless stream

broadcast_to stream, data["data"]
::ActionCable.server.broadcast stream, data["data"]
end
end)

0 comments on commit ed5ab22

Please sign in to comment.