blob: b5fc8129194ddbb4d1372fc1ce8e250d2ee540eb (
plain) (
tree)
|
|
module ActionCable
module Channel
module Redis
extend ActiveSupport::Concern
included do
on_unsubscribe :unsubscribe_from_all_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 ]
pubsub.subscribe(redis_channel, &callback)
logger.info "#{channel_name} subscribed to incoming actions from #{redis_channel}"
end
def unsubscribe_from_all_channels
if @_redis_channels
@_redis_channels.each do |redis_channel, callback|
pubsub.unsubscribe_proc(redis_channel, callback)
logger.info "#{channel_name} unsubscribed from incoming actions #{redis_channel}"
end
end
end
protected
def default_subscription_callback(channel)
-> (message) do
logger.info "Received a message over the redis channel: #{channel}"
broadcast ActiveSupport::JSON.decode(message)
end
end
end
end
end
|