SSEConnection

Inherits: RefCounted < Object

Represents an active Server-Sent Events connection.

Description

SSEConnection manages an individual Server-Sent Events (SSE) connection to a client. It handles the formatting and sending of SSE events according to the W3C specification. SSEConnection objects are created automatically by HTTPServer when a route handler calls HTTPResponse.start_sse().

You typically don't create SSEConnection objects directly. Instead, use HTTPServer.send_sse_event() to send events to connections identified by their connection ID.

# Listen for new SSE connections
HTTPServer.sse_connection_opened.connect(func(conn_id, path, headers):
    print("New SSE client: ", conn_id)
    # Send a welcome event
    HTTPServer.send_sse_event(conn_id, "welcome", "Connected!")
)

Tutorials

Properties

int

connection_id

0

String

path

""

Methods

void

close_connection()

bool

is_connection_active() const

Error

send_data(data: String)

Error

send_event(event_type: String, data: String, event_id: String = "")


Property Descriptions

int connection_id = 0 🔗

  • void set_connection_id(value: int)

  • int get_connection_id()

The unique identifier for this SSE connection.


String path = "" 🔗

The request path that initiated this SSE connection.


Method Descriptions

void close_connection() 🔗

Closes the SSE connection. This will disconnect the client and free resources.


bool is_connection_active() const 🔗

Returns true if the connection is still active.


Error send_data(data: String) 🔗

Sends data to the client without specifying an event type. This is equivalent to calling send_event() with an empty event type.

Multi-line data is automatically formatted according to the SSE specification.


Error send_event(event_type: String, data: String, event_id: String = "") 🔗

Sends a Server-Sent Event to the client. The event_type specifies the type of event (used by client-side EventSource.addEventListener), data is the event payload, and event_id is an optional event ID for client-side tracking.

Multi-line data is automatically split and formatted correctly. Returns @GlobalScope.OK on success.