2015-06-28 14:24:50 -04:00
|
|
|
module ActionCable
|
|
|
|
module Server
|
|
|
|
module Broadcasting
|
2015-06-28 14:36:10 -04:00
|
|
|
def broadcast(channel, message)
|
|
|
|
broadcaster_for(channel).broadcast(message)
|
|
|
|
end
|
|
|
|
|
2015-06-28 14:24:50 -04:00
|
|
|
def broadcaster_for(channel)
|
|
|
|
Broadcaster.new(self, channel)
|
|
|
|
end
|
|
|
|
|
2015-06-28 14:36:10 -04:00
|
|
|
private
|
|
|
|
def redis_for_threads
|
|
|
|
@redis_for_threads ||= Redis.new(redis_config)
|
|
|
|
end
|
2015-06-28 14:24:50 -04:00
|
|
|
|
2015-06-28 14:36:10 -04:00
|
|
|
class Broadcaster
|
|
|
|
def initialize(server, channel)
|
|
|
|
@server, @channel = server, channel
|
|
|
|
end
|
2015-06-28 14:24:50 -04:00
|
|
|
|
2015-06-28 14:38:05 -04:00
|
|
|
def broadcast(message)
|
|
|
|
server.logger.info "[ActionCable] Broadcasting to #{channel}: #{message}"
|
|
|
|
broadcast_without_logging(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def broadcast_without_logging(message)
|
2015-06-28 14:36:10 -04:00
|
|
|
server.redis_for_threads.publish channel, message.to_json
|
|
|
|
end
|
2015-06-28 14:24:50 -04:00
|
|
|
|
2015-06-28 14:36:10 -04:00
|
|
|
private
|
|
|
|
attr_reader :server, :channel
|
2015-06-28 14:24:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|