Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
sse.c File Reference

Server-Sent Events (SSE) implementation. More...

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
#include "csilk/csilk.h"
#include "csilk/core/internal.h"
Include dependency graph for sse.c:

Functions

static void on_sse_write (uv_write_t *req, int status)
 SSE write completion callback.
 
void csilk_sse_init (csilk_ctx_t *c)
 Initialize an SSE connection with proper headers and status.
 
void csilk_sse_send (csilk_ctx_t *c, const char *event, const char *data)
 Send an SSE event (optional event type + data).
 
void csilk_sse_close (csilk_ctx_t *c)
 Close the SSE connection.
 

Detailed Description

Server-Sent Events (SSE) implementation.

Function Documentation

◆ csilk_sse_close()

void csilk_sse_close ( csilk_ctx_t *  c)

Close the SSE connection.

Closes the underlying libuv stream handle for the SSE client connection. Performs a graceful close via uv_close(). Any pending write requests will complete before the handle is fully closed.

Parameters
cThe request context (must be in SSE mode with an active client).

◆ csilk_sse_init()

void csilk_sse_init ( csilk_ctx_t *  c)

Initialize an SSE connection with proper headers and status.

Initialise a Server-Sent Events connection.

Sets the Content-Type, Cache-Control, Connection, and X-Accel-Buffering headers, marks the context as an SSE connection (is_sse = 1), and writes the raw HTTP 200 OK response headers directly to the client socket.

Parameters
cThe request context.
Note
After calling csilk_sse_init(), the connection is established and the caller should not call csilk_next() โ€” SSE hijacks the socket.
Warning
The headers are written as a raw string bypassing the normal response pipeline. The connection remains open for the lifetime of the SSE stream.

◆ csilk_sse_send()

void csilk_sse_send ( csilk_ctx_t *  c,
const char *  event,
const char *  data 
)

Send an SSE event (optional event type + data).

Send an SSE event (or comment) to the client.

Formats and writes a Server-Sent Event message to the client. If an event type is provided, an "event:" line is emitted. The data is written as a "data:" line. Both are terminated by an empty line ("\n") as required by the SSE specification (RFC 8895 ยง2).

Parameters
cThe request context (must be in SSE mode).
eventOptional event type string (e.g. "message", "update"). May be NULL, in which case no "event:" line is emitted.
dataThe event payload string. May be NULL (produces a "data:" line with no content). Must not contain embedded "\n\n" sequences unless multi-line data is intended per SSE spec.
Note
Each send allocates a new buffer and write request. For high- frequency event streams, consider batching or a pooled allocator.
Warning
This function does NOT check for backpressure. If the client reads slower than the server writes, the kernel buffer may fill and writes will fail with EAGAIN (reported via on_sse_write).

◆ on_sse_write()

static void on_sse_write ( uv_write_t *  req,
int  status 
)
static

SSE write completion callback.

Called by libuv after an asynchronous write to the SSE client socket completes (or fails). On failure, logs the error via CSILK_LOG_E. In either case frees the write request and the associated buffer.

Parameters
reqThe completed uv_write_t request. req->data points to the heap-allocated buffer that was written.
status0 on success, negative on error (UV_E* codes).