From 4f36bc66e640cdd4e42ab1174cb61cd7e3b17b0d Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 19 Jan 2015 15:40:32 +0530 Subject: Add support for redis channels --- lib/action_cable/channel/redis.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/action_cable/channel/redis.rb (limited to 'lib/action_cable/channel/redis.rb') diff --git a/lib/action_cable/channel/redis.rb b/lib/action_cable/channel/redis.rb new file mode 100644 index 0000000000..215cc3c975 --- /dev/null +++ b/lib/action_cable/channel/redis.rb @@ -0,0 +1,32 @@ +module ActionCable + module Channel + + module Redis + extend ActiveSupport::Concern + + included do + on_unsubscribe :unsubscribe_from_redis_channels + end + + def subscribe_to(redis_channel, callback = nil) + @_redis_channels ||= [] + @_redis_channels << redis_channel + + callback ||= -> (message) { broadcast ActiveSupport::JSON.decode(message) } + redis.pubsub.subscribe(redis_channel, &callback) + end + + protected + def unsubscribe_from_redis_channels + if @_redis_channels + @_redis_channels.each { |channel| @connection.pubsub.unsubscribe(channel) } + end + end + + def redis + @connection.redis + end + end + + end +end \ No newline at end of file -- cgit v1.2.3