The streaming plugin adds support for streaming responses from roda using the stream
method:
plugin :streaming route do |r| stream do |out| ['a', 'b', 'c'].each{|v| out << v; sleep 1} end end
In order for streaming to work, any webservers used in front of the roda app must not buffer responses.
The stream method takes the following options:
:callback |
A callback proc to call when the connection is closed. |
:loop |
Whether to call the stream block continuously until the connection is closed. |
:async |
Whether to call the stream block in a separate thread (default: false). Only supported on Ruby 2.3+. |
:queue |
A queue object to use for asynchronous streaming (default: ‘SizedQueue.new(10)`). |
If the :loop option is used, you can override the handle_stream_error method to change how exceptions are handled during streaming. This method is passed the exception and the stream. By default, this method just reraises the exception, but you can choose to output the an error message to the stream, before raising:
def handle_stream_error(e, out) out << 'ERROR!' raise e end
Ignore errors completely while streaming:
def handle_stream_error(e, out) end
or handle the errors in some other way.