pub struct Event { /* private fields */ }Expand description
Server-sent event
Implementations§
Source§impl Event
impl Event
Sourcepub const DEFAULT_KEEP_ALIVE: Self
pub const DEFAULT_KEEP_ALIVE: Self
Default keep-alive event
Sourcepub fn raw<T>(bytes: T) -> Selfwhere
T: Into<Bytes>,
pub fn raw<T>(bytes: T) -> Selfwhere
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");Sourcepub fn into_data_writer(self) -> EventDataWriter
pub fn into_data_writer(self) -> EventDataWriter
Use this Event as a EventDataWriter to write custom data.
Self::datacan be used as a shortcut to writestrdataSelf::json_datacan be used as a shortcut to writejsondata
Turn it into an Event again using EventDataWriter::into_event.
Sourcepub fn data<T>(self, data: T) -> Self
pub fn data<T>(self, data: T) -> Self
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.
Sourcepub fn json_data<T>(self, data: T) -> Result<Self, Error>where
T: Serialize,
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.
Sourcepub fn comment<T>(self, comment: T) -> Self
pub fn comment<T>(self, comment: T) -> Self
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.
Sourcepub fn event<T>(self, event: T) -> Self
pub fn event<T>(self, event: T) -> Self
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
eventcontains any newlines or carriage returns. - Panics if this function has already been called on this event.
Sourcepub fn retry(self, duration: Duration) -> Self
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.
Sourcepub fn id<T>(self, id: T) -> Self
pub fn id<T>(self, id: T) -> Self
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
idcontains any newlines, carriage returns or null characters. - Panics if this function has already been called on this event.