Skip to main content

Event

Struct Event 

Source
pub struct Event { /* private fields */ }
Expand description

Server-sent event

Implementations§

Source§

impl Event

Source

pub const DEFAULT_KEEP_ALIVE: Self

Default keep-alive event

Source

pub fn raw<T>(bytes: T) -> Self
where T: Into<Bytes>,

Create an event whose payload is emitted on the wire exactly as provided.

The given bytes are sent as a single body frame, verbatim: there is no data: framing, no validation of field syntax, and no terminating empty line is added. If the event needs to be understood by a standard EventSource client, the caller is responsible for providing the complete event including its terminator, such as "data: hi\n\n".

This is meant as an escape hatch for nonstandard event formats (such as clients that don’t accept the usual SSE framing) while still keeping Sse’s handling of the content-type/cache-control headers and its KeepAlive support.

Like any other event, a raw event resets the keep-alive timer when sent through Sse::keep_alive.

Note that calling further builder methods, such as Event::comment, after raw will append their fields after the raw bytes and re-add a terminating newline. That’s allowed, but usually not what you want.

§Example
use axum::response::sse::Event;

let event = Event::raw("data: hi\n\n");
Source

pub fn into_data_writer(self) -> EventDataWriter

Use this Event as a EventDataWriter to write custom data.

Turn it into an Event again using EventDataWriter::into_event.

Source

pub fn data<T>(self, data: T) -> Self
where T: AsRef<str>,

Set the event’s data field(s) (data: <content>)

Newlines in data will automatically be broken across data: fields.

This corresponds to MessageEvent’s data field.

Note that events with an empty data field will be ignored by the browser.

§Panics

Panics if any data has already been written before.

Source

pub fn json_data<T>(self, data: T) -> Result<Self, Error>
where T: Serialize,

Set the event’s data field to a value serialized as unformatted JSON (data: <content>).

This corresponds to MessageEvent’s data field.

§Panics

Panics if any data has already been written before.

Source

pub fn comment<T>(self, comment: T) -> Self
where T: AsRef<str>,

Set the event’s comment field (:<comment-text>).

This field will be ignored by most SSE clients.

Unlike other functions, this function can be called multiple times to add many comments.

§Panics

Panics if comment contains any newlines or carriage returns, as they are not allowed in comments.

Source

pub fn event<T>(self, event: T) -> Self
where T: AsRef<str>,

Set the event’s name field (event:<event-name>).

This corresponds to the type parameter given when calling addEventListener on an EventSource. For example, .event("update") should correspond to .addEventListener("update", ...). If no event type is given, browsers will fire a message event instead.

§Panics
  • Panics if event contains any newlines or carriage returns.
  • Panics if this function has already been called on this event.
Source

pub fn retry(self, duration: Duration) -> Self

Set the event’s retry timeout field (retry: <timeout>).

This sets how long clients will wait before reconnecting if they are disconnected from the SSE endpoint. Note that this is just a hint: clients are free to wait for longer if they wish, such as if they implement exponential backoff.

§Panics

Panics if this function has already been called on this event.

Source

pub fn id<T>(self, id: T) -> Self
where T: AsRef<str>,

Set the event’s identifier field (id:<identifier>).

This corresponds to MessageEvent’s lastEventId field. If no ID is in the event itself, the browser will set that field to the last known message ID, starting with the empty string.

§Panics
  • Panics if id contains any newlines, carriage returns or null characters.
  • Panics if this function has already been called on this event.
Source

pub fn finalize(self) -> Bytes

Turns the event to Bytes ready to be sent

If the events is already finalized just returns the Bytes again

Trait Implementations§

Source§

impl Clone for Event

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Event

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Event

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Event

§

impl RefUnwindSafe for Event

§

impl Send for Event

§

impl Sync for Event

§

impl Unpin for Event

§

impl UnsafeUnpin for Event

§

impl UnwindSafe for Event

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more