A client for Server-Sent Events, the streams that AI chat APIs, live feeds and many dashboards send their updates on.
{:ok, response} = Stampede.SSE.get("http://localhost:4000/api/updates")
[%{event: "message", data: data, at: at} | _] = response.eventsRequests go through Stampede.HTTP, so each VU keeps its connection to a
server open between them, as a browser does. The response has the
:status, the :headers, the raw :body, and the :events read from
it, in order. Each event has:
:event- its type,"message"unless the server set another:data- its data, with the lines of a multi-line event joined by"\n":id- the last id the server sent, ornil:at- when it arrived, in milliseconds from sending the request
Events follow the HTML standard's rules for text/event-stream. An event
the stream ended in the middle of, without the blank line after it, still
counts, since some servers end that way.
Metrics
Inside a run, requests record:
"sse.request"- from sending the request to the end of the stream"sse.first_event"- from sending the request to the first event"sse.events"- how many events arrived"sse.request.errors"- requests that failed, or whose response had a status of 400 or more
Summary
Types
Functions
Opens a stream with a GET request. See request/3.
Opens a stream with a POST request. See request/3.
Sends a method request to url and reads the events it streams back,
until the server ends the stream.
Returns {:ok, response} whatever the status, and {:error, reason} when
the stream failed, such as a Mint.TransportError when the connection
closed before the end.
Options
:headers- a list of{name, value}headers. Anacceptheader oftext/event-streamis added unless one is given.:body- the body, as iodata:json- a term to send as JSON instead of a:body:cookies- whentrue, uses the cookies ofStampede.HTTP's:cookiesoption, so a stream can come after signing in. Defaults tofalse.:timeout- how long the whole stream may take, in milliseconds. Defaults to60_000.:metric- the prefix of the metrics to record, ornilto record nothing. Defaults to"sse".:transport_opts- options for:gen_tcpor:ssl, given toMint.HTTP.connect/4