pub struct Event { /* private fields */ }Expand description
A Server-Sent Event to be delivered to a connected client.
Uses a builder pattern. id and event name are required at construction
and validated — \n and \r are rejected.
§Examples
use modo::sse::Event;
let event = Event::new("evt_01", "message")?.data("Hello, world!");
let event = Event::new(modo::id::short(), "status")?.json(&status)?;
let event = Event::new(modo::id::short(), "update")?.html("<div>new</div>");Implementations§
Source§impl Event
impl Event
Sourcepub fn new(
id: impl Into<String>,
event: impl Into<String>,
) -> Result<Self, Error>
pub fn new( id: impl Into<String>, event: impl Into<String>, ) -> Result<Self, Error>
Create a new event. Both id and event are required.
idmaps to the SSEid:field — used by clients forLast-Event-IDon reconnection.eventmaps to the SSEevent:field — clients listen for specific event types (e.g.,eventSource.addEventListener("message", handler)or HTMXhx-trigger="sse:message").
§Errors
Returns an error if id or event contain \n or \r.
Sourcepub fn data(self, data: impl Into<String>) -> Self
pub fn data(self, data: impl Into<String>) -> Self
Set the data payload as a plain string.
Multi-line strings are handled automatically per the SSE spec — each
line gets its own data: prefix. The browser reassembles them with \n.
Sourcepub fn json<T: Serialize>(self, data: &T) -> Result<Self, Error>
pub fn json<T: Serialize>(self, data: &T) -> Result<Self, Error>
Set the data payload as JSON-serialized data.
Replaces any previous data.
§Errors
Returns an error if JSON serialization fails.
Sourcepub fn html(self, html: impl Into<String>) -> Self
pub fn html(self, html: impl Into<String>) -> Self
Set the data payload as an HTML fragment.
Semantically identical to data(). Communicates intent
for HTMX partial rendering use cases.
Sourcepub fn retry(self, duration: Duration) -> Self
pub fn retry(self, duration: Duration) -> Self
Set the reconnection delay hint for the client.
Serialized as milliseconds in the SSE retry: field. Tells the browser
how long to wait before reconnecting after a disconnect.
Sourcepub fn event_name(&self) -> &str
pub fn event_name(&self) -> &str
Returns the event name.