Sin descripción

yhirose fee592c2c2 Ignore a leading UTF-8 BOM in an SSE stream hace 22 horas
.github 3517f92e2f ci: remove the temporary windows-without-SSL flaky failure reporter hace 4 semanas
benchmark 6494edd8c0 Add static-file, large-body and TLS workloads to the A/B benchmark hace 1 mes
cmake 0d7d637466 Fix zstd detection in installed httplibConfig.cmake (#2453) hace 4 meses
docker f6aee694e2 Log the response length for static files in the docker server hace 22 horas
docs-src 885ed54b10 Unify spacing and wording in the Japanese docs pages hace 22 horas
docs-util 7178f451a4 "Building a Desktop LLM App with cpp-httplib" (#2403) hace 6 meses
example f2b338ae2e Drop the empty Accept entry from the example's invalid list hace 1 mes
scripts deb520e26b Update version files with sed -i.bak, which GNU sed accepts too hace 2 semanas
test fee592c2c2 Ignore a leading UTF-8 BOM in an SSE stream hace 22 horas
.clang-format 260422b7d7 Format code hace 6 años
.gitattributes 177d8420a1 Added .gitattributes file to prevent git from changing line endings (#1872) hace 2 años
.gitignore 644cda7032 Update .gitignore hace 1 mes
.pre-commit-config.yaml f83d06538b Track Homebrew's clang-format version instead of a fixed pin hace 1 mes
CMakeLists.txt 5a202d3d5f Added a feature test to auto enable/disable CPPHTTPLIB_USE_NON_BLOCKI… (#2578) hace 2 semanas
Dockerfile f4cc542d4b Fix Dockerfile problem with CMD hace 1 año
LICENSE ca7b942196 Changed license to MIT hace 8 años
README-sse.md 0715c2739e Enforce a minimum SSE reconnect wait to avoid a busy loop (#2592) hace 1 semana
README-stream.md 05f7478523 Fix README samples and statements that no longer match the code hace 22 horas
README-websocket.md 05f7478523 Fix README samples and statements that no longer match the code hace 22 horas
README.md 05f7478523 Fix README samples and statements that no longer match the code hace 22 horas
docker-compose.yml ba638ff38e Update Docker support hace 2 años
generate_module.py 1942e0ef01 Add C++ modules support (#2291) hace 8 meses
httplib.h fee592c2c2 Ignore a leading UTF-8 BOM in an SSE stream hace 22 horas
justfile 29ceccecd6 Add A/B throughput benchmark for comparing two refs hace 2 meses
meson.build d3ff68d28d Always use meson option non_blocking_getaddrinfo (#2537) hace 1 mes
meson_options.txt c2002f6e06 Make loading system certificates from the Keychain on macOS an opt-out feature (#2377) hace 7 meses
split.py 1942e0ef01 Add C++ modules support (#2291) hace 8 meses

README-sse.md

SSEClient - Server-Sent Events Client

A simple, EventSource-like SSE client for C++11.

Features

  • Auto-reconnect: Automatically reconnects on connection loss
  • Last-Event-ID: Sends last received ID on reconnect for resumption
  • retry field: Respects server's reconnect interval
  • Event types: Supports custom event types via on_event()
  • Async support: Run in background thread with start_async()
  • C++11 compatible: No C++14/17/20 features required

Quick Start

httplib::Client cli("http://localhost:8080");
httplib::sse::SSEClient sse(cli, "/events");

sse.on_message([](const httplib::sse::SSEMessage &msg) {
    std::cout << "Event: " << msg.event << std::endl;
    std::cout << "Data: " << msg.data << std::endl;
});

sse.start();  // Blocking, with auto-reconnect

API Reference

SSEMessage

struct SSEMessage {
    std::string event;  // Event type (default: "message")
    std::string data;   // Event payload
    std::string id;     // Event ID
};

SSEClient

Constructor

// Basic
SSEClient(Client &client, const std::string &path);

// With custom headers
SSEClient(Client &client, const std::string &path, const Headers &headers);

Event Handlers

// Called for all events (or events without a specific handler)
sse.on_message([](const SSEMessage &msg) { });

// Called for specific event types
sse.on_event("update", [](const SSEMessage &msg) { });
sse.on_event("delete", [](const SSEMessage &msg) { });

// Called when connection is established
sse.on_open([]() { });

// Called on connection errors
sse.on_error([](httplib::Error err) { });

Configuration

// Set reconnect interval (default: 3000ms, minimum: 100ms)
sse.set_reconnect_interval(5000);

// Set max reconnect attempts (default: 0 = unlimited)
sse.set_max_reconnect_attempts(10);

// Update headers at any time (thread-safe)
sse.set_headers({{"Authorization", "Bearer new_token"}});

Control

// Blocking start with auto-reconnect
sse.start();

// Non-blocking start (runs in background thread)
sse.start_async();

// Stop the client (thread-safe)
sse.stop();

State

bool connected = sse.is_connected();
const std::string &id = sse.last_event_id();

Examples

Basic Usage

httplib::Client cli("http://localhost:8080");
httplib::sse::SSEClient sse(cli, "/events");

sse.on_message([](const httplib::sse::SSEMessage &msg) {
    std::cout << msg.data << std::endl;
});

sse.start();

With Custom Event Types

httplib::sse::SSEClient sse(cli, "/events");

sse.on_event("notification", [](const httplib::sse::SSEMessage &msg) {
    std::cout << "Notification: " << msg.data << std::endl;
});

sse.on_event("update", [](const httplib::sse::SSEMessage &msg) {
    std::cout << "Update: " << msg.data << std::endl;
});

sse.start();

Async with Stop

httplib::sse::SSEClient sse(cli, "/events");

sse.on_message([](const httplib::sse::SSEMessage &msg) {
    std::cout << msg.data << std::endl;
});

sse.start_async();  // Returns immediately

// ... do other work ...

sse.stop();  // Stop when done

With Custom Headers (e.g., Authentication)

httplib::Headers headers = {
    {"Authorization", "Bearer token123"}
};

httplib::sse::SSEClient sse(cli, "/events", headers);
sse.start();

Refreshing Auth Token on Reconnect

httplib::sse::SSEClient sse(cli, "/events",
    {{"Authorization", "Bearer " + get_token()}});

// Preemptively refresh token on each successful connection
sse.on_open([&sse]() {
    sse.set_headers({{"Authorization", "Bearer " + get_token()}});
});

// Or reactively refresh on auth failure (401 triggers reconnect)
sse.on_error([&sse](httplib::Error) {
    sse.set_headers({{"Authorization", "Bearer " + refresh_token()}});
});

sse.start();

Error Handling

sse.on_error([](httplib::Error err) {
    std::cerr << "Error: " << httplib::to_string(err) << std::endl;
});

sse.set_reconnect_interval(1000);
sse.set_max_reconnect_attempts(5);

sse.start();

SSE Protocol

The client parses SSE format according to the W3C specification:

event: custom-type
id: 123
data: {"message": "hello"}

data: simple message

: this is a comment (ignored)