Ei kuvausta

yhirose ad5839f0d1 Add retry logic to BenchmarkTest test on Windows 10 kuukautta sitten
.github ad5839f0d1 Add retry logic to BenchmarkTest test on Windows 10 kuukautta sitten
benchmark 0c08c378d7 Simplify benchmark 1 vuosi sitten
cmake db561f5552 [cmake] FindBrotli: do not add `Brotli::` targets if they already exist (#2249) 1 vuosi sitten
docker 40f7985e02 Update copyright year 10 kuukautta sitten
example 40f7985e02 Update copyright year 10 kuukautta sitten
test 02dfb97fd6 Add `Expect: 100-continue` support 10 kuukautta sitten
.clang-format 260422b7d7 Format code 7 vuotta sitten
.gitattributes 177d8420a1 Added .gitattributes file to prevent git from changing line endings (#1872) 2 vuotta sitten
.gitignore 8bba34eebc Add New Streaming API support (#2281) 11 kuukautta sitten
.pre-commit-config.yaml c551e97297 Add .pre-commit-config.yaml 1 vuosi sitten
CMakeLists.txt f85f30a637 Apple frameworks: match CoreFoundation and CFNetwork linkage (#2317) 10 kuukautta sitten
Dockerfile f4cc542d4b Fix Dockerfile problem with CMD 1 vuosi sitten
LICENSE ca7b942196 Changed license to MIT 9 vuotta sitten
README-sse.md 51b704b902 Implement SSEClient (#2308) 11 kuukautta sitten
README-stream.md 1d36013fc3 Update README 11 kuukautta sitten
README.md 40f7985e02 Update copyright year 10 kuukautta sitten
docker-compose.yml ba638ff38e Update Docker support 2 vuotta sitten
httplib.h 02dfb97fd6 Add `Expect: 100-continue` support 10 kuukautta sitten
meson.build f85f30a637 Apple frameworks: match CoreFoundation and CFNetwork linkage (#2317) 10 kuukautta sitten
meson_options.txt 2de4c59bc2 Add Zstd support through meson (#2293) 11 kuukautta sitten
split.py c3613c6977 Update the split.py file (#2295) 11 kuukautta sitten

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)
sse.set_reconnect_interval(5000);

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

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();

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)