aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib/action_cable/storage_adapter/redis.rb
blob: 3e0ede057a1def7a9a42b36aaefc1848cde311eb (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
38
39
40
41
require 'em-hiredis'
require 'redis'

module ActionCable
  module StorageAdapter
    class Redis < Base
      def broadcast(channel, payload)
        redis_conn.publish(channel, payload)
      end

      def subscribe(channel, message_callback, success_callback = nil)
        hi_redis_conn.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

      # The redis instance used for broadcasting. Not intended for direct user use.
      def redis_conn
        @broadcast ||= ::Redis.new(@server.config.config_opts)
      end

      # The EventMachine Redis instance used by the pubsub adapter.
      def hi_redis_conn
        @hi_redis_conn ||= 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_conn
        @redis_conn ||= ::Redis.new(@server.config.cable)
      end
    end
  end
end