diff options
Diffstat (limited to 'lib/action_cable/server/broadcasting.rb')
-rw-r--r-- | lib/action_cable/server/broadcasting.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/action_cable/server/broadcasting.rb b/lib/action_cable/server/broadcasting.rb new file mode 100644 index 0000000000..b0e51b8ba8 --- /dev/null +++ b/lib/action_cable/server/broadcasting.rb @@ -0,0 +1,39 @@ +module ActionCable + module Server + module Broadcasting + def broadcast(broadcasting, message) + broadcaster_for(broadcasting).broadcast(message) + end + + def broadcast_without_logging(broadcasting, message) + broadcaster_for(broadcasting).broadcast_without_logging(message) + end + + def broadcaster_for(broadcasting) + Broadcaster.new(self, broadcasting) + end + + def broadcasting_redis + @broadcasting_redis ||= Redis.new(redis_config) + end + + private + class Broadcaster + attr_reader :server, :broadcasting + + def initialize(server, broadcasting) + @server, @broadcasting = server, broadcasting + end + + def broadcast(message) + server.logger.info "[ActionCable] Broadcasting to #{broadcasting}: #{message}" + broadcast_without_logging(message) + end + + def broadcast_without_logging(message) + server.broadcasting_redis.publish broadcasting, message.to_json + end + end + end + end +end
\ No newline at end of file |