blob: fda55ec45dd939c06045a3e5d9adcf754e73f3df (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
module ActionCable
module Channel
module Redis
extend ActiveSupport::Concern
included do
on_unsubscribe :unsubscribe_from_redis_channels
delegate :pubsub, to: :connection
end
def subscribe_to(redis_channel, callback = nil)
callback ||= default_subscription_callback(redis_channel)
@_redis_channels ||= []
@_redis_channels << [ redis_channel, callback ]
logger.info "[ActionCable] Subscribing to the redis channel: #{redis_channel}"
pubsub.subscribe(redis_channel, &callback)
end
protected
def unsubscribe_from_redis_channels
if @_redis_channels
@_redis_channels.each do |channel, callback|
logger.info "[ActionCable] Unsubscribing from the redis channel: #{channel}"
pubsub.unsubscribe_proc(channel, callback)
end
end
end
def default_subscription_callback(channel)
-> (message) do
logger.info "[ActionCable] Received a message over the redis channel: #{channel} (#{message})"
broadcast ActiveSupport::JSON.decode(message)
end
end
end
end
end
|