pub struct Event { /* private fields */ }
Expand description
Server-sent event
Implementations§
source§impl Event
impl Event
sourcepub fn data<T>(self, data: T) -> Event
pub fn data<T>(self, data: T) -> Event
Set the event’s data 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
data
contains any carriage returns, as they cannot be transmitted over SSE. - Panics if
data
orjson_data
have already been called.
sourcepub fn json_data<T>(self, data: T) -> Result<Event, Error>where
T: Serialize,
pub fn json_data<T>(self, data: T) -> Result<Event, 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 data
or json_data
have already been called.
sourcepub fn comment<T>(self, comment: T) -> Event
pub fn comment<T>(self, comment: T) -> Event
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) -> Event
pub fn event<T>(self, event: T) -> Event
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.
sourcepub fn retry(self, duration: Duration) -> Event
pub fn retry(self, duration: Duration) -> Event
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) -> Event
pub fn id<T>(self, id: T) -> Event
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.