Add your stream handler to the list of stream handlers in the protocol options:
{ok, Pid} = cowboy:start_clear(ListenerName,
[{port, Port}],
#{
env => #{dispatch => Dispatch},
stream_handlers => [my_stream_h, cowboy_compress_h, cowboy_stream_h]
}
),
A stream handler
- should implement the behavior
cowboy_stream
. - inserts itself into the request/response stack and can modify both incoming requests and outgoing repsonses
Answering to myself, the routes should have a separate, non-streaming handler module, like so:
with just
init/2
in it (note thatcowboy_loop
):That seems to be sufficient to set up the state for streaming, which is to be done via
cowboy_req:cast/2
anddata/4
in your streaming_h.erl.