Skip to content

Commit

Permalink
Make broadcasting a concern
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed Jun 29, 2015
1 parent f61467e commit e1a99a8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
17 changes: 0 additions & 17 deletions lib/action_cable/broadcaster.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/action_cable/server.rb
@@ -1,6 +1,7 @@
module ActionCable
module Server
autoload :Base, 'action_cable/server/base'
autoload :Broadcasting, 'action_cable/server/broadcasting'
autoload :Worker, 'action_cable/server/worker'
end
end
10 changes: 2 additions & 8 deletions lib/action_cable/server/base.rb
@@ -1,6 +1,8 @@
module ActionCable
module Server
class Base
include ActionCable::Server::Broadcasting

cattr_accessor(:logger, instance_reader: true) { Rails.logger }

attr_accessor :registered_channels, :redis_config, :log_tags
Expand Down Expand Up @@ -49,14 +51,6 @@ 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
Expand Down
28 changes: 28 additions & 0 deletions lib/action_cable/server/broadcasting.rb
@@ -0,0 +1,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

0 comments on commit e1a99a8

Please sign in to comment.