Skip to content

Commit

Permalink
Whispering backport for Action Cable
Browse files Browse the repository at this point in the history
  • Loading branch information
lHydra authored and Georgiy Melnikov committed Jun 9, 2024
1 parent 61e5d2f commit e1e1f4e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/anycable/rails/action_cable_ext/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def stop_periodic_timers

def stream_from(broadcasting, _callback = nil, **opts)
whispering = opts.delete(:whisper)
if whispering
self.class.state_attr_accessor(:whisper_stream) unless respond_to?(:whisper_stream)
self.whisper_stream = broadcasting
end

return super unless anycabled?

broadcasting = String(broadcasting)
Expand All @@ -50,6 +55,12 @@ def stop_all_streams
connection.anycable_socket.unsubscribe_from_all identifier
end

def whisper(payload)
return unless whisper_stream

broadcast_to whisper_stream, payload
end

private

def anycabled?
Expand Down
15 changes: 15 additions & 0 deletions lib/anycable/rails/action_cable_ext/whisper_stream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "action_cable"

ActionCable::Connection::Subscriptions.prepend(Module.new do
def execute_command(data)
return whisper(data) if data["command"] == "whisper"

super
end

def whisper(data)
find(data).whisper(ActiveSupport::JSON.decode(data["data"]))
end
end)
1 change: 1 addition & 0 deletions lib/anycable/rails/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Rails
module Ext
autoload :JWT, "anycable/rails/ext/jwt"
autoload :SignedStreams, "anycable/rails/ext/signed_streams"
autoload :WhisperStreams, "anycable/rails/ext/whisper_stream"
end
end
end

0 comments on commit e1e1f4e

Please sign in to comment.