Expand description
A futures_codec
that encodes and decodes Server-Sent Event/Event Sourcing streams.
It emits or serializes full messages, and the meta-message retry:
.
§Examples
use sse_codec::{decode_stream, Event};
use futures::stream::TryStreamExt; // for try_next()
let response = surf::get("https://some-site.com/events").await?;
let mut events = decode_stream(response);
while let Some(event) = events.try_next().await? {
println!("incoming: {:?}", event);
match event {
Event::Retry { retry } => {
// change a retry timer value or something
}
Event::Message { event, .. } if event == "stop" => {
break;
}
Event::Message { id, event, data } => {
if let Some(id) = id {
// change the last event ID
}
// handle event here
}
}
}
Structs§
- SSECodec
- Encoder/decoder for server-sent event streams.
Enums§
- Error
- Errors that may occur while encoding or decoding server-sent event messages.
- Event
- An “event”, either an incoming message or some meta-action that needs to be applied to the stream.
Functions§
- decode_
stream - Parse messages from an
AsyncRead
, returning a stream ofEvent
s. - encode_
stream - Encode
Event
s into anAsyncWrite
.
Type Aliases§
- Decode
Stream - Type of a decoding stream, returned from
decode_stream()
. - Encode
Stream - Type of an encoding stream, returned from
encode_stream()
.