KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
Data Structures | Functions
sse.h File Reference
#include <keel/response.h>
#include <stddef.h>
Include dependency graph for sse.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  KlSse
 SSE (Server-Sent Events) stream handle. More...
 

Functions

int kl_sse_begin (KlResponse *res, KlSse *sse)
 Begin an SSE stream.
 
int kl_sse_event (KlSse *sse, const char *event, const char *data, size_t data_len, const char *id)
 Send an SSE event.
 
int kl_sse_comment (KlSse *sse, const char *text, size_t len)
 Send an SSE comment (keep-alive ping pattern).
 
int kl_sse_end (KlSse *sse)
 End the SSE stream.
 

Function Documentation

◆ kl_sse_begin()

int kl_sse_begin ( KlResponse res,
KlSse sse 
)

Begin an SSE stream.

Sets Content-Type: text/event-stream, Cache-Control: no-cache, and starts a chunked streaming response at status 200.

Parameters
resResponse to stream on.
sseSSE handle to initialize (caller-owned).
Returns
0 on success, -1 on failure.

◆ kl_sse_event()

int kl_sse_event ( KlSse sse,
const char *  event,
const char *  data,
size_t  data_len,
const char *  id 
)

Send an SSE event.

Writes event:/id:/data: fields followed by a blank line. NULL event or id omits that field. Multiline data is auto-split on '
' — each line gets its own "data: " prefix.

Parameters
sseSSE handle from kl_sse_begin.
eventEvent type (NULL to omit).
dataEvent data (may contain '
' for multiline).
data_lenLength of data in bytes.
idEvent ID (NULL to omit).
Returns
0 on success, -1 on write error.

◆ kl_sse_comment()

int kl_sse_comment ( KlSse sse,
const char *  text,
size_t  len 
)

Send an SSE comment (keep-alive ping pattern).

Writes ": text\n". Comments are ignored by SSE clients but keep the connection alive through proxies.

Parameters
sseSSE handle.
textComment text.
lenLength of text in bytes.
Returns
0 on success, -1 on write error.

◆ kl_sse_end()

int kl_sse_end ( KlSse sse)

End the SSE stream.

Sends the final zero-length chunk via kl_response_end_stream.

Parameters
sseSSE handle.
Returns
0 on success, -1 on error.