Skip to main content

Module stream

Module stream 

Source
Available on crate feature stream only.
Expand description

Async adapter: turn a byte Stream into a Stream of SseEvents.

This module is only compiled with the stream feature, which pulls in futures-core (and nothing else). It wraps any Stream<Item = Result<Bytes, E>>, where Bytes: AsRef<[u8]>, and drives an SseParser across chunk boundaries.

use futures::stream::{self, StreamExt};
use stream_rs::stream::SseStream;

let body = stream::iter(vec![
    Ok::<_, std::io::Error>(b"data: hello\n".to_vec()),
    Ok(b"\n".to_vec()),
]);
let mut events = SseStream::new(body);
while let Some(ev) = events.next().await {
    let ev = ev.unwrap();
    println!("{}", ev.data);
}

Structsยง

SseStream
Adapts a fallible byte stream into a stream of parsed SseEvents.