diff options
author | Matthew Draper <matthew@trebex.net> | 2016-01-20 16:03:13 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2016-01-20 16:09:20 +1030 |
commit | 56a9341689942146d350ff931497956a15152bdb (patch) | |
tree | b667657e63aed80d1c04b8fd08223812c5e870e7 /actioncable/lib/action_cable/subscription_adapter/redis.rb | |
parent | 8f208e0f90cafda767005bc1fce63709414e84dd (diff) | |
parent | ae31da20cd250154c951b67d5625fc71ac27e2f1 (diff) | |
download | rails-56a9341689942146d350ff931497956a15152bdb.tar.gz rails-56a9341689942146d350ff931497956a15152bdb.tar.bz2 rails-56a9341689942146d350ff931497956a15152bdb.zip |
Merge pull request #22950 from maclover7/adapterize-storage-actioncable
Adapterize storage for ActionCable
Diffstat (limited to 'actioncable/lib/action_cable/subscription_adapter/redis.rb')
-rw-r--r-- | actioncable/lib/action_cable/subscription_adapter/redis.rb | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/actioncable/lib/action_cable/subscription_adapter/redis.rb b/actioncable/lib/action_cable/subscription_adapter/redis.rb new file mode 100644 index 0000000000..d149f28b1f --- /dev/null +++ b/actioncable/lib/action_cable/subscription_adapter/redis.rb @@ -0,0 +1,37 @@ +gem 'em-hiredis', '~> 0.3.0' +gem 'redis', '~> 3.0' +require 'em-hiredis' +require 'redis' + +module ActionCable + module SubscriptionAdapter + class Redis < Base # :nodoc: + def broadcast(channel, payload) + redis_connection_for_broadcasts.publish(channel, payload) + end + + def subscribe(channel, message_callback, success_callback = nil) + redis_connection_for_subscriptions.pubsub.subscribe(channel, &message_callback).tap do |result| + result.callback(&success_callback) if success_callback + end + end + + def unsubscribe(channel, message_callback) + hi_redis_conn.pubsub.unsubscribe_proc(channel, message_callback) + end + + private + def redis_connection_for_subscriptions + @redis_connection_for_subscriptions ||= EM::Hiredis.connect(@server.config.cable[:url]).tap do |redis| + redis.on(:reconnect_failed) do + @logger.info "[ActionCable] Redis reconnect failed." + end + end + end + + def redis_connection_for_broadcasts + @redis_connection_for_broadcasts ||= ::Redis.new(@server.config.cable) + end + end + end +end |