pub struct Event { /* private fields */ }Expand description
Represents a Server-Sent Event that can be sent to clients.
Implementations§
Source§impl Event
impl Event
Sourcepub fn new<T: Serialize>(data: &T) -> Result<Self, Error>
pub fn new<T: Serialize>(data: &T) -> Result<Self, Error>
Creates a new SSE event from JSON-serializable data.
This helper is available when the json feature is enabled and returns an
error instead of panicking if serialization fails.
§Examples
use http_kit::sse::Event;
use serde::Serialize;
#[derive(Serialize)]
struct Message { text: String }
let msg = Message { text: "Hello".to_string() };
let event = Event::new(&msg)?;Sourcepub fn from_data<T: Into<String>>(data: T) -> Self
pub fn from_data<T: Into<String>>(data: T) -> Self
Creates a new SSE event from string data.
§Examples
use http_kit::sse::Event;
let event = Event::from_data("Hello, World!");Sourcepub fn data<T>(&self) -> Result<T, Error>where
T: for<'de> Deserialize<'de>,
pub fn data<T>(&self) -> Result<T, Error>where
T: for<'de> Deserialize<'de>,
Deserializes the event data as JSON.
This helper is available when the json feature is enabled.
§Errors
Returns an error if the data cannot be deserialized as the specified type.
Sourcepub fn with_id<T: Into<String>>(self, id: T) -> Self
pub fn with_id<T: Into<String>>(self, id: T) -> Self
Sets the event ID.
§Examples
use http_kit::sse::Event;
let event = Event::from_data("Hello").with_id("msg-123");Sourcepub fn with_event<T: Into<String>>(self, event: T) -> Self
pub fn with_event<T: Into<String>>(self, event: T) -> Self
Sets the event type.
§Examples
use http_kit::sse::Event;
let event = Event::from_data("Hello").with_event("message");Sourcepub fn with_retry(self, retry: u64) -> Self
pub fn with_retry(self, retry: u64) -> Self
Sets the retry duration in milliseconds.
§Examples
use http_kit::sse::Event;
let event = Event::from_data("Hello").with_retry(5000);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Event
impl RefUnwindSafe for Event
impl Send for Event
impl Sync for Event
impl Unpin for Event
impl UnwindSafe for Event
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more