aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/channel/redis.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2015-01-19 15:40:32 +0530
committerPratik Naik <pratiknaik@gmail.com>2015-01-19 15:40:32 +0530
commit4f36bc66e640cdd4e42ab1174cb61cd7e3b17b0d (patch)
tree0deb0b9527824b502ae01a2bcd2a6c515a26495a /lib/action_cable/channel/redis.rb
parent568599dd206301b8fde9b75f4913de4caed65967 (diff)
downloadrails-4f36bc66e640cdd4e42ab1174cb61cd7e3b17b0d.tar.gz
rails-4f36bc66e640cdd4e42ab1174cb61cd7e3b17b0d.tar.bz2
rails-4f36bc66e640cdd4e42ab1174cb61cd7e3b17b0d.zip
Add support for redis channels
Diffstat (limited to 'lib/action_cable/channel/redis.rb')
-rw-r--r--lib/action_cable/channel/redis.rb32
1 files changed, 32 insertions, 0 deletions
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