aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/connection/registry.rb
blob: 121f87f9f034a5c68de4f0d74d0d889384070586 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module ActionCable
  module Connection
    module Registry
      extend ActiveSupport::Concern

      included do
        class_attribute :identifiers
        self.identifiers = Set.new
      end

      module ClassMethods
        def identified_by(*identifiers)
          self.identifiers += identifiers
        end
      end

      def register_connection
        if connection_identifier.present?
          callback = -> (message) { process_registry_message(message) }
          @_internal_redis_subscriptions ||= []
          @_internal_redis_subscriptions << [ internal_redis_channel, callback ]

          pubsub.subscribe(internal_redis_channel, &callback)
          logger.info "[ActionCable] Registered connection (#{connection_identifier})"
          puts "[ActionCable] Registered connection: #{connection_identifier}(#{internal_redis_channel})"
        end
      end

      def internal_redis_channel
        "action_cable/#{connection_identifier}"
      end

      def connection_identifier
        @connection_identifier ||= connection_gid identifiers.map { |id| instance_variable_get("@#{id}")}
      end

      def connection_gid(ids)
        ids.map {|o| o.to_global_id.to_s }.sort.join(":")
      end

      def cleanup_internal_redis_subscriptions
        if @_internal_redis_subscriptions.present?
          @_internal_redis_subscriptions.each { |channel, callback| pubsub.unsubscribe_proc(channel, callback) }
        end
      end

      private
        def process_registry_message(message)
          message = ActiveSupport::JSON.decode(message)

          case message['type']
          when 'disconnect'
            logger.info "[ActionCable] Removing connection (#{connection_identifier})"
            @websocket.close
          end
        rescue Exception => e
          logger.error "[ActionCable] There was an exception - #{e.class}(#{e.message})"
          logger.error e.backtrace.join("\n")

          handle_exception
        end

    end
  end
end