Event

Struct Event 

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

Represents a Server-Sent Event that can be sent to clients.

Implementations§

Source§

impl Event

Source

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)?;
Source

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!");
Source

pub const fn id(&self) -> Option<&str>

Returns the event ID if set.

Source

pub const fn event(&self) -> Option<&str>

Returns the event type if set.

Source

pub const fn retry(&self) -> Option<u64>

Returns the retry duration in milliseconds if set.

Source

pub const fn text_data(&self) -> &str

Returns the raw text data of the event.

Source

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.

Source

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");
Source

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");
Source

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);
Source

pub fn encode(&self) -> String

Encodes the event as an SSE-formatted string.

The output follows the SSE specification format:

  • event: <type> (optional)
  • data: <data>
  • id: <id> (optional)
  • retry: <milliseconds> (optional)
  • Empty line to end the event

Trait Implementations§

Source§

impl Debug for Event

Source§

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

Formats the value using the given formatter. 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 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.