class SseRailsEngine::Connection

Attributes

channels[RW]
stream[RW]

Public Class Methods

new(io, env) click to toggle source
# File lib/sse_rails_engine/connection.rb, line 8
def initialize(io, env)
  @socket = io
  @socket.write(sse_header)
  @socket.flush
  @stream = ActionController::Live::SSE.new(io)
  @channels = requested_channels(env)
end

Public Instance Methods

write(name, data) click to toggle source
# File lib/sse_rails_engine/connection.rb, line 16
def write(name, data)
  return if filtered?(name)
  @stream.write(data, event: name)
  @socket.flush
end

Private Instance Methods

filtered?(channel) click to toggle source
# File lib/sse_rails_engine/connection.rb, line 24
def filtered?(channel)
  return false if @channels.empty? || channel == Manager::HEARTBEAT_EVENT
  !@channels.include?(channel)
end
requested_channels(env) click to toggle source
# File lib/sse_rails_engine/connection.rb, line 29
def requested_channels(env)
  Rack::Utils.parse_query(env['QUERY_STRING']).fetch('channels', []).split(',').flatten
end