diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2015-04-13 21:45:03 -0500 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2015-04-13 21:45:03 -0500 |
commit | 5743cf30ff7714c7aa83133b350af22840473733 (patch) | |
tree | a25c6144eb264f52a4af772676c575d1d27cf743 /lib | |
parent | 07bcf2dcc93e6f391413add86f96f0ed72392a06 (diff) | |
download | rails-5743cf30ff7714c7aa83133b350af22840473733.tar.gz rails-5743cf30ff7714c7aa83133b350af22840473733.tar.bz2 rails-5743cf30ff7714c7aa83133b350af22840473733.zip |
Add Broadcaster to publish to redis channels
Diffstat (limited to 'lib')
-rw-r--r-- | lib/action_cable.rb | 1 | ||||
-rw-r--r-- | lib/action_cable/broadcaster.rb | 18 | ||||
-rw-r--r-- | lib/action_cable/server.rb | 12 |
3 files changed, 31 insertions, 0 deletions
diff --git a/lib/action_cable.rb b/lib/action_cable.rb index 1926201f1f..26b3980deb 100644 --- a/lib/action_cable.rb +++ b/lib/action_cable.rb @@ -25,4 +25,5 @@ module ActionCable autoload :Connection, 'action_cable/connection' autoload :RemoteConnection, 'action_cable/remote_connection' autoload :RemoteConnections, 'action_cable/remote_connections' + autoload :Broadcaster, 'action_cable/broadcaster' end diff --git a/lib/action_cable/broadcaster.rb b/lib/action_cable/broadcaster.rb new file mode 100644 index 0000000000..b2352876e9 --- /dev/null +++ b/lib/action_cable/broadcaster.rb @@ -0,0 +1,18 @@ +module ActionCable + class Broadcaster + attr_reader :server, :channel, :redis + delegate :logger, to: :server + + def initialize(server, channel) + @server = server + @channel = channel + @redis = @server.threaded_redis + end + + def broadcast(message) + redis.publish channel, message.to_json + logger.info "[ActionCable] Broadcasting to channel (#{channel}): #{message}" + end + + end +end diff --git a/lib/action_cable/server.rb b/lib/action_cable/server.rb index bfadcee229..70b0610e92 100644 --- a/lib/action_cable/server.rb +++ b/lib/action_cable/server.rb @@ -38,10 +38,22 @@ module ActionCable end end + def threaded_redis + @threaded_redis ||= Redis.new(redis_config) + end + def remote_connections @remote_connections ||= RemoteConnections.new(self) end + def broadcaster_for(channel) + Broadcaster.new(self, channel) + end + + def broadcast(channel, message) + broadcaster_for(channel).broadcast(message) + end + def connection_identifiers @connection_class.identifiers end |