| 1 |
= cowboy_req:stream_events(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:stream_events - Stream events |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
stream_events(Events, IsFin, Req :: cowboy_req:req()) -> ok |
| 12 |
|
| 13 |
Events :: Event | [Event] |
| 14 |
IsFin :: fin | nofin |
| 15 |
|
| 16 |
Event :: #{ |
| 17 |
comment => iodata(), |
| 18 |
data => iodata(), |
| 19 |
event => iodata() | atom(), |
| 20 |
id => iodata(), |
| 21 |
retry => non_neg_integer() |
| 22 |
} |
| 23 |
---- |
| 24 |
|
| 25 |
Stream events. |
| 26 |
|
| 27 |
This function should only be used for `text/event-stream` |
| 28 |
responses when using server-sent events. Cowboy will |
| 29 |
automatically encode the given events to their text |
| 30 |
representation. |
| 31 |
|
| 32 |
This function may be called as many times as needed after |
| 33 |
initiating a response using the |
| 34 |
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)] |
| 35 |
function. |
| 36 |
|
| 37 |
The second argument indicates if this call is the final |
| 38 |
call. Use the `nofin` value until you know no more data |
| 39 |
will be sent. The final call should use `fin` (possibly |
| 40 |
with an empty data value) or be a call to the |
| 41 |
link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)] |
| 42 |
function. |
| 43 |
|
| 44 |
Note that not using `fin` for the final call is not an |
| 45 |
error; Cowboy will take care of it when the request |
| 46 |
handler terminates if needed. Depending on the resource |
| 47 |
it may however be more efficient to do it as early as |
| 48 |
possible. |
| 49 |
|
| 50 |
You do not need to handle HEAD requests specifically as |
| 51 |
Cowboy will ensure no data is sent when you call this function. |
| 52 |
|
| 53 |
== Arguments |
| 54 |
|
| 55 |
Events:: |
| 56 |
|
| 57 |
Events to be sent. All fields are optional. |
| 58 |
|
| 59 |
IsFin:: |
| 60 |
|
| 61 |
A flag indicating whether this is the final piece of data |
| 62 |
to be sent. |
| 63 |
|
| 64 |
Req:: |
| 65 |
|
| 66 |
The Req object. |
| 67 |
|
| 68 |
== Return value |
| 69 |
|
| 70 |
The atom `ok` is always returned. It can be safely ignored. |
| 71 |
|
| 72 |
== Changelog |
| 73 |
|
| 74 |
* *2.5*: Function introduced. |
| 75 |
|
| 76 |
== Examples |
| 77 |
|
| 78 |
.Stream events |
| 79 |
[source,erlang] |
| 80 |
---- |
| 81 |
Req = cowboy_req:stream_reply(200, #{ |
| 82 |
<<"content-type">> => <<"text/event-stream">> |
| 83 |
}, Req0), |
| 84 |
cowboy_req:stream_events(#{ |
| 85 |
id => <<"comment-123">>, |
| 86 |
event => <<"add_comment">>, |
| 87 |
data => <<"Hello,\n\nI noticed something wrong in ...">> |
| 88 |
}, nofin, Req), |
| 89 |
timer:sleep(1000), |
| 90 |
cowboy_req:stream_events(#{ |
| 91 |
event => <<"debug">>, |
| 92 |
data => io_lib:format("An error occurred: ~p~n", [Error]) |
| 93 |
}, fin, Req). |
| 94 |
---- |
| 95 |
|
| 96 |
== See also |
| 97 |
|
| 98 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 99 |
link:man:cowboy_req:stream_reply(3)[cowboy_req:stream_reply(3)], |
| 100 |
link:man:cowboy_req:stream_body(3)[cowboy_req:stream_body(3)], |
| 101 |
link:man:cowboy_req:stream_trailers(3)[cowboy_req:stream_trailers(3)] |