aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib/action_cable/subscription_adapter/redis.rb
blob: e42ab2a03fbb2ba1641e47d454585e841a2e6d7b (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
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)
        redis_connection_for_subscriptions.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