Skip to main content

Module sse

Module sse 

Source
Expand description

Server-Sent Events (SSE) support.

This module provides types and utilities for implementing server-sent events as defined in the HTML Living Standard.

§Overview

Server-Sent Events allow servers to push data to clients over HTTP. The connection stays open and the server can send events whenever new data is available.

§Example

use fastapi_core::sse::{SseEvent, SseResponse};
use fastapi_core::Response;
use asupersync::stream;

async fn event_stream() -> Response {
    // Create an async stream of events
    let events = stream::iter(vec![
        SseEvent::message("Hello!"),
        SseEvent::new("World!")
            .event_type("greeting")
            .id("1"),
    ]);

    SseResponse::new(events).into_response()
}

§Event Format

Each event is sent as a text block with the following format:

event: <event-type>\n
id: <id>\n
retry: <milliseconds>\n
data: <data-line-1>\n
data: <data-line-2>\n
\n
  • event: Optional event type (default is “message”)
  • id: Optional event ID for resumption
  • retry: Optional reconnection time in milliseconds
  • data: The actual payload (required, can be multiple lines)

§Keep-Alive

Use SseEvent::comment() to send keep-alive comments that prevent connection timeouts without sending actual events.

§Cancellation

SSE streams integrate with asupersync’s cancellation. When the client disconnects, the stream will be cancelled at the next checkpoint.

Structs§

SseConfig
Configuration for SSE responses.
SseEvent
A Server-Sent Event.
SseResponse
Builder for creating SSE responses.
SseStream
A wrapper that converts an async stream of SSE events into formatted bytes.

Functions§

sse_response
Convenience function to create an SSE response from an iterator.