Class: Mistri::Sinks::SSE

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/sinks/sse.rb

Overview

Writes events as Server-Sent Events to any IO-like object: a Rack streaming body, an ActionController::Live stream, a socket. Pure formatting, the outbound counterpart of the Mistri::SSE decoder.

response.headers["Content-Type"] = "text/event-stream"
agent.run(input, &Mistri::Sinks::SSE.new(response.stream))

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ SSE

Returns a new instance of SSE.



14
15
16
# File 'lib/mistri/sinks/sse.rb', line 14

def initialize(io)
  @io = io
end

Instance Method Details

#call(event) ⇒ Object



18
19
20
21
# File 'lib/mistri/sinks/sse.rb', line 18

def call(event)
  @io.write("event: #{event.type}\ndata: #{JSON.generate(event.to_h)}\n\n")
  @io.flush if @io.respond_to?(:flush)
end

#to_procObject



23
# File 'lib/mistri/sinks/sse.rb', line 23

def to_proc = method(:call).to_proc