Skip to main content

SseStream

Struct SseStream 

Source
pub struct SseStream<T: TryStream> { /* private fields */ }
Expand description

An asynchronous stream wrapper that parses SSE events from an underlying byte stream.

Implementations§

Source§

impl<T: TryStream> SseStream<T>

Source

pub fn disconnected() -> Self

Creates a new, disconnected SseStream.

A disconnected stream will immediately yield None (terminated) if polled. This constructor is primarily useful when you need to store the SseStream inside a struct before the network connection is established.

To make the stream active, you must attach an inner stream using attach().

§Example
let mut stream = SseStream::disconnected();

// ... later, when the network is ready:
let byte_stream = fetch_http_stream().await;
stream.attach(byte_stream);
Source

pub fn with_decoder(decoder: SseDecoder) -> Self

Creates a disconnected stream initialized with a custom decoder.

See the disconnected() function for more information.

Source

pub fn new(inner: T) -> Self

Creates a new SseStream wrapping the provided inner stream.

Source

pub fn take_decoder(self) -> SseDecoder

Consumes the stream and returns the underlying state-machine decoder.

Source

pub fn is_closed(&self) -> bool

Returns true if the stream is currently disconnected.

Source

pub fn last_event_id(&self) -> Option<&Arc<str>>

Returns the current Last-Event-ID parsed by the underlying decoder.

Source

pub fn close(&mut self)

Disconnects the inner stream while retaining the underlying parser’s state.

This drops the active network connection but safely preserves the most recently parsed Last-Event-ID within the decoder. This is the standard method to temporarily pause a stream or handle a dropped connection, allowing you to later resume exactly where you left off.

  • To close the stream and inject a new ID for the next connection, use close_with_id().
  • To close the stream and completely wipe the session state, use close_and_clear().
Source

pub fn close_and_clear(&mut self)

Disconnects the stream and completely purges the underlying parser’s state.

This drops the inner stream, clears all internal byte buffers, and permanently drops the currently tracked Last-Event-ID. It effectively returns the SseStream to the exact state it was in when initially created via disconnected().

  • To close the stream and keep the current ID, use close().
  • To close the stream and inject a new ID, use close_with_id().
Source

pub fn close_with_id(&mut self, id: Option<Arc<str>>)

Disconnects the inner stream and explicitly overrides the underlying decoder’s Last-Event-ID in preparation for a future connection.

This is particularly useful in async contexts where you must drop the active stream, inject a new ID, and then yield back to the runtime before establishing a new network connection. The injected ID will be available immediately via last_event_id().

  • To close the stream and keep the current ID, use close().
  • To close the stream and completely wipe the session state, use close_and_clear().
Source

pub fn attach(&mut self, inner: T)

Attaches a new inner stream to resume processing events.

This method resets the underlying parser’s buffers but retains the most recently received Last-Event-ID. It is the standard way to recover from a dropped network connection without missing any events.

If you need to manually inject a saved Last-Event-ID (e.g., when recovering an offline session from a database), use attach_with_id() instead.

Source

pub fn attach_with_id(&mut self, inner: T, id: Option<Arc<str>>)

Attaches a new inner stream to resume processing, explicitly overriding the Last-Event-ID in the underlying decoder.

This method is primarily used when recovering an offline session where you need to initialize the stream with a saved ID right as you provide the new HTTP response stream.

If you just want to resume a dropped stream using the ID that the decoder has already tracked automatically, use attach().

Trait Implementations§

Source§

impl<T: TryStream> FusedStream for SseStream<T>
where T::Ok: Buf,

Source§

fn is_terminated(&self) -> bool

Returns true if the stream should no longer be polled.
Source§

impl<T: TryStream> Stream for SseStream<T>
where T::Ok: Buf,

Source§

type Item = Result<SseEvent, SseStreamError<<T as TryStream>::Error>>

Values yielded by the stream.
Source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more
Source§

impl<'__pin, T: TryStream> Unpin for SseStream<T>
where PinnedFieldsOf<__Origin<'__pin, T>>: Unpin,

Auto Trait Implementations§

§

impl<T> Freeze for SseStream<T>
where T: Freeze, <T as TryStream>::Ok: Freeze,

§

impl<T> RefUnwindSafe for SseStream<T>

§

impl<T> Send for SseStream<T>
where T: Send, <T as TryStream>::Ok: Send,

§

impl<T> Sync for SseStream<T>
where T: Sync, <T as TryStream>::Ok: Sync,

§

impl<T> UnsafeUnpin for SseStream<T>
where T: UnsafeUnpin, <T as TryStream>::Ok: UnsafeUnpin,

§

impl<T> UnwindSafe for SseStream<T>
where T: UnwindSafe, <T as TryStream>::Ok: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T, E> TryStream for S
where S: Stream<Item = Result<T, E>> + ?Sized,

Source§

type Ok = T

The type of successful values yielded by this future
Source§

type Error = E

The type of failures yielded by this future
Source§

fn try_poll_next( self: Pin<&mut S>, cx: &mut Context<'_>, ) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>

Poll this TryStream as if it were a Stream. Read more