std1.0.0-beta-7 SafeC docs

Module http::sse

#include <std/http/sse.h> source

Server-Sent Events (the WHATWG text/event-stream format, what the browser's EventSource reads).

Server: a handler takes its connection over with SseStream::start and writes events until it is done or the client goes away (send* returns 0):

struct HttpResponse events(&HttpRequest req, &HttpCtx ctx) {
    struct SseStream s = std::SseStream::start(ctx.conn, null);
    while (s.send_event("tick", "{\"n\":1}", null)) { std::thread_sleep_ms(1000UL); }
    s.close();
    return std::HttpResponse::new(200);     // ignored: the connection was detached
}

The stream blocks the worker thread that runs it; give long-lived streams their own thread (or a server with enough workers).

Client: SseClient connects to an http:// or https:// URL and yields parsed events; SseParser is the incremental parser underneath, usable on any byte source.

Structs

SseStream
SseEvent
SseParser
SseClient