Expand description
Server-sent events (text/event-stream): a parser, the events of a response, and their data: decoded as JSON.
Response::events gives the events of a response as they arrive; Events::json decodes each data: payload
to a type of your own. A data: [DONE] event or the end of the body ends the stream. Decoder is the parser
underneath, for a body read some other way.
use futures_util::StreamExt;
#[derive(serde::Deserialize)]
struct Chunk {
id: String,
}
let response = client
.request(timvw_rust_runtime::Method::POST, "https://api.example.com/v1/stream")
.json(&serde_json::json!({"stream": true}))
.send()
.await?;
let mut chunks = response.events().json::<Chunk>();
while let Some(chunk) = chunks.next().await {
println!("{}", chunk?.id);
}Structs§
- Decoder
- An incremental
text/event-streamparser, following the HTML standard: lines end with CR, LF or CRLF, a line starting with:is a comment, an empty line dispatches the event, and an event withoutdata:is not dispatched. Feed it the body as it arrives. - Event
- One server-sent event, before its data is decoded.
- Events
- The events of a
text/event-streambody, as they come. Adata: [DONE]event ends it, as does the end of the body or a failure reading it (yielded asErrfirst); the body is dropped then, which frees the connection. - Json
Events - The events of a stream with their
data:decoded as JSON:Events::json.
Constants§
- DONE
- The
data:value that ends a stream.