runtime: native SSE streaming — http_sse_open/send/close

Add Server-Sent Events support to the El runtime. El v2 handlers can now
hold HTTP connections open and push events in real time.

New builtins in el_seed.c:
  __http_conn_fd()          — retrieve raw fd from thread-local set by worker
  __http_sse_open(fd)       — send SSE headers (text/event-stream), keep-alive
  __http_sse_send(fd, data) — write "data: <data>\n\n" frame
  __http_sse_close(fd)      — close the connection fd

http_worker_v2 in legacy/el_runtime.c now:
  - stashes the fd via el_seed_set_http_conn_fd() before calling the handler
  - detects the "__sse__" sentinel return value to skip http_send_response
    and skip close(fd) — SSE handler took ownership of the fd
  - clears the thread-local after the handler returns

El wrappers added to runtime/http.el:
  http_conn_fd() http_sse_open(fd) http_sse_send(fd, data)
  http_sse_close(fd) http_sse_sentinel()
This commit is contained in:
Will Anderson
2026-05-03 17:15:37 -05:00
parent 3e5130e98d
commit 4ae42ee7db
5 changed files with 184 additions and 9 deletions
+20
View File
@@ -81,6 +81,26 @@ void __http_serve_v2(el_val_t port, el_val_t handler_name);
* headers_json: JSON object literal like {"Content-Type":"text/plain"} or "{}" */
el_val_t __http_response(el_val_t status, el_val_t headers_json, el_val_t body);
/* ── HTTP SSE — Server-Sent Events streaming ─────────────────────────────── */
/* Returns the raw file descriptor for the current HTTP connection.
* Valid only inside an http_serve_v2 handler before it returns.
* Returns -1 if called outside a handler context. */
el_val_t __http_conn_fd(void);
/* Sends SSE response headers on conn_id (the fd from __http_conn_fd),
* keeping the connection open for streaming. Returns 1 on success, 0 on
* write failure. Call once at the start of an SSE handler. */
el_val_t __http_sse_open(el_val_t conn_id);
/* Writes one SSE event frame: "data: <data>\n\n". data must not contain
* newlines. Returns 1 on success, 0 if the client disconnected. */
el_val_t __http_sse_send(el_val_t conn_id, el_val_t data);
/* Closes the SSE connection. The handler must return http_sse_sentinel()
* so the HTTP worker does not double-close the fd. */
el_val_t __http_sse_close(el_val_t conn_id);
/* ── Threading ───────────────────────────────────────────────────────────── */
/* Create a thread that calls the named El function with a String argument.