The roda-sse
Roda plugin provides a streaming interface for
server-sent events. Each stream is wrapped in an Async reactor
and events are sent asyncronously via tasks. The roda-sse plugin sets
appropriate SSE headers, handles disconnection errors, ensures
streams are properly closed and a last_event_id
helper.
gem install roda-sse
Source code is available on GitHub at https://github.com/havenwood/roda-sse
See the examples/ directory for an example app.
Enable the roda-sse
plugin in your Roda app.
class App < Roda
plugin :sse
end
In your routing block, you can then use an r.sse do |stream|
block to stream output.
r.sse do |stream|
puts "Last-Event-ID header: #{last_event_id.inspect}"
stream << "data: hello\n\n"
stream << "data: world\n\n"
end