aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/server/broadcasting.rb
blob: 691ec1b486c8203fa75de16b7c6348df2f1b34ba (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
module ActionCable
  module Server
    module Broadcasting
      def broadcast(channel, message)
        broadcaster_for(channel).broadcast(message)
      end

      def broadcaster_for(channel)
        Broadcaster.new(self, channel)
      end

      private
        def redis_for_threads
          @redis_for_threads ||= Redis.new(redis_config)
        end      

        class Broadcaster
          def initialize(server, channel)
            @server, @channel = server, channel
          end

          def broadcast(message, log: true)
            server.logger.info "[ActionCable] Broadcasting to #{channel}: #{message}" if log
            server.redis_for_threads.publish channel, message.to_json
          end

          private
            attr_reader :server, :channel
        end
    end
  end
end