A simple, EventSource-like SSE client for C++11.
on_event()start_async()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
struct SSEMessage { std::string event; // Event type (default: "message") std::string data; // Event payload std::string id; // Event ID };
// Basic SSEClient(Client &client, const std::string &path); // With custom headers SSEClient(Client &client, const std::string &path, const Headers &headers);
// 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) { });
// Set reconnect interval (default: 3000ms) sse.set_reconnect_interval(5000); // Set max reconnect attempts (default: 0 = unlimited) sse.set_max_reconnect_attempts(10);
// Blocking start with auto-reconnect sse.start(); // Non-blocking start (runs in background thread) sse.start_async(); // Stop the client (thread-safe) sse.stop();
bool connected = sse.is_connected(); const std::string &id = sse.last_event_id();
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();
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();
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
httplib::Headers headers = { {"Authorization", "Bearer token123"} }; httplib::sse::SSEClient sse(cli, "/events", headers); sse.start();
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();
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)