aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/server/broadcasting.rb
blob: 682064571f64950a1750612a7a370f11c68241ec (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
module ActionCable
  module Server
    module Broadcasting
      def broadcaster_for(channel)
        Broadcaster.new(self, channel)
      end

      def broadcast(channel, message)
        broadcaster_for(channel).broadcast(message)
      end

      class Broadcaster
        attr_reader :server, :channel, :redis
        delegate :logger, to: :server

        def initialize(server, channel)
          @server, @channel = server, channel
          @redis = @server.threaded_redis
        end

        def broadcast(message)
          logger.info "[ActionCable] Broadcasting to #{channel}: #{message}"
          redis.publish channel, message.to_json
        end
      end
    end
  end
end