DeterministicESPAsyncWebServer 1.2.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sse.h File Reference

Layer 6 (Presentation) – Server-Sent Events connection pool. More...

#include "DetWebServerConfig.h"
#include "transport.h"

Go to the source code of this file.

Classes

struct  SseConn
 SSE connection state stored in sse_pool[]. More...
 

Functions

void sse_init ()
 Initialise all SSE pool slots to inactive.
 
SseConnsse_alloc (uint8_t slot_id, const char *path)
 Allocate an SseConn and bind it to a TCP slot.
 
SseConnsse_find (uint8_t slot_id)
 Find the SseConn for a given TCP slot, or nullptr.
 
void sse_free (uint8_t slot_id)
 Free the SseConn associated with a TCP slot.
 
bool sse_write (SseConn *sse, const char *data, const char *event, const char *id)
 Write one SSE event record to a client.
 

Variables

SseConn sse_pool [MAX_SSE_CONNS]
 Pool of SSE connection state, one per MAX_SSE_CONNS.
 

Detailed Description

Layer 6 (Presentation) – Server-Sent Events connection pool.

SSE (RFC 8895 / W3C EventSource) is a long-lived HTTP GET response with Content-Type: text/event-stream. After the initial headers the connection stays open indefinitely; the server pushes newline-delimited event records at any time.

Event record format (RFC 8895 ยง9.2.6)

[event: <name>\n]
[id: <id>\n]
data: <payload>\n
\n

Each SseConn occupies one TCP slot from conn_pool[] for the lifetime of the subscription. The total number of simultaneous SSE connections is capped at MAX_SSE_CONNS.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file sse.h.

Function Documentation

◆ sse_init()

void sse_init ( )

Initialise all SSE pool slots to inactive.

Called once from DetWebServer::begin().

Definition at line 16 of file sse.cpp.

References MAX_SSE_CONNS, SseConn::sse_id, and sse_pool.

Referenced by DetWebServer::begin(), and DetWebServer::stop().

◆ sse_alloc()

SseConn * sse_alloc ( uint8_t  slot_id,
const char *  path 
)

Allocate an SseConn and bind it to a TCP slot.

Parameters
slot_idTCP slot that just received the SSE subscription request.
pathURL path the client subscribed to (stored for broadcast).
Returns
Pointer to the allocated SseConn, or nullptr if the pool is full.

Definition at line 25 of file sse.cpp.

References SseConn::active, MAX_PATH_LEN, MAX_SSE_CONNS, SseConn::path, SseConn::slot_id, SseConn::sse_id, and sse_pool.

◆ sse_find()

SseConn * sse_find ( uint8_t  slot_id)

Find the SseConn for a given TCP slot, or nullptr.

Parameters
slot_idTCP connection slot index.

Definition at line 43 of file sse.cpp.

References MAX_SSE_CONNS, and sse_pool.

Referenced by DetWebServer::handle().

◆ sse_free()

void sse_free ( uint8_t  slot_id)

Free the SseConn associated with a TCP slot.

Parameters
slot_idTCP connection slot index.

Definition at line 53 of file sse.cpp.

References MAX_SSE_CONNS, SseConn::sse_id, and sse_pool.

◆ sse_write()

bool sse_write ( SseConn sse,
const char *  data,
const char *  event,
const char *  id 
)

Write one SSE event record to a client.

Formats and sends event: ...\nid: ...\ndata: ...\n\n. Any optional field may be nullptr to omit it. data must not be nullptr.

The caller must call tcp_output() on the underlying PCB afterwards if immediate delivery is needed.

Parameters
sseSSE connection.
dataEvent data (required).
eventEvent name (optional).
idEvent ID (optional).
Returns
true on success, false if the TCP slot is not active.

Definition at line 66 of file sse.cpp.

References CONN_ACTIVE, conn_pool, TcpConn::pcb, SseConn::slot_id, SSE_BUF_SIZE, and TcpConn::state.

Variable Documentation

◆ sse_pool

SseConn sse_pool[MAX_SSE_CONNS]
extern

Pool of SSE connection state, one per MAX_SSE_CONNS.

Definition at line 14 of file sse.cpp.

Referenced by sse_alloc(), sse_find(), sse_free(), and sse_init().