|
| 1 | +# SSEClient - Server-Sent Events Client |
| 2 | + |
| 3 | +A simple, EventSource-like SSE client for C++11. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +-**Auto-reconnect**: Automatically reconnects on connection loss |
| 8 | +-**Last-Event-ID**: Sends last received ID on reconnect for resumption |
| 9 | +-**retry field**: Respects server's reconnect interval |
| 10 | +-**Event types**: Supports custom event types via `on_event()` |
| 11 | +-**Async support**: Run in background thread with `start_async()` |
| 12 | +-**C++11 compatible**: No C++14/17/20 features required |
| 13 | + |
| 14 | +## Quick Start |
| 15 | + |
| 16 | +```cpp |
| 17 | +httplib::Client cli("http://localhost:8080"); |
| 18 | +httplib::sse::SSEClient sse(cli, "/events"); |
| 19 | + |
| 20 | +sse.on_message([](const httplib::sse::SSEMessage &msg){ |
| 21 | + std::cout << "Event: " << msg.event << std::endl; |
| 22 | + std::cout << "Data: " << msg.data << std::endl; |
| 23 | +}); |
| 24 | + |
| 25 | +sse.start(); // Blocking, with auto-reconnect |
| 26 | +``` |
| 27 | +
|
| 28 | +## API Reference |
| 29 | +
|
| 30 | +### SSEMessage |
| 31 | +
|
| 32 | +```cpp |
| 33 | +struct SSEMessage{ |
| 34 | + std::string event; // Event type (default: "message") |
| 35 | + std::string data; // Event payload |
| 36 | + std::string id; // Event ID |
| 37 | +}; |
| 38 | +``` |
| 39 | + |
| 40 | +### SSEClient |
| 41 | + |
| 42 | +#### Constructor |
| 43 | + |
| 44 | +```cpp |
| 45 | +// Basic |
| 46 | +SSEClient(Client &client, const std::string &path); |
| 47 | + |
| 48 | +// With custom headers |
| 49 | +SSEClient(Client &client, const std::string &path, const Headers &headers); |
| 50 | +``` |
| 51 | +
|
| 52 | +#### Event Handlers |
| 53 | +
|
| 54 | +```cpp |
| 55 | +// Called for all events (or events without a specific handler) |
| 56 | +sse.on_message([](const SSEMessage &msg){}); |
| 57 | +
|
| 58 | +// Called for specific event types |
| 59 | +sse.on_event("update", [](const SSEMessage &msg){}); |
| 60 | +sse.on_event("delete", [](const SSEMessage &msg){}); |
| 61 | +
|
| 62 | +// Called when connection is established |
| 63 | +sse.on_open([](){}); |
| 64 | +
|
| 65 | +// Called on connection errors |
| 66 | +sse.on_error([](httplib::Error err){}); |
| 67 | +``` |
| 68 | + |
| 69 | +#### Configuration |
| 70 | + |
| 71 | +```cpp |
| 72 | +// Set reconnect interval (default: 3000ms) |
| 73 | +sse.set_reconnect_interval(5000); |
| 74 | + |
| 75 | +// Set max reconnect attempts (default: 0 = unlimited) |
| 76 | +sse.set_max_reconnect_attempts(10); |
| 77 | +``` |
| 78 | + |
| 79 | +#### Control |
| 80 | + |
| 81 | +```cpp |
| 82 | +// Blocking start with auto-reconnect |
| 83 | +sse.start(); |
| 84 | + |
| 85 | +// Non-blocking start (runs in background thread) |
| 86 | +sse.start_async(); |
| 87 | + |
| 88 | +// Stop the client (thread-safe) |
| 89 | +sse.stop(); |
| 90 | +``` |
| 91 | + |
| 92 | +#### State |
| 93 | + |
| 94 | +```cpp |
| 95 | +bool connected = sse.is_connected(); |
| 96 | +const std::string &id = sse.last_event_id(); |
| 97 | +``` |
| 98 | + |
| 99 | +## Examples |
| 100 | + |
| 101 | +### Basic Usage |
| 102 | + |
| 103 | +```cpp |
| 104 | +httplib::Client cli("http://localhost:8080"); |
| 105 | +httplib::sse::SSEClient sse(cli, "/events"); |
| 106 | + |
| 107 | +sse.on_message([](const httplib::sse::SSEMessage &msg){ |
| 108 | + std::cout << msg.data << std::endl; |
| 109 | +}); |
| 110 | + |
| 111 | +sse.start(); |
| 112 | +``` |
| 113 | +
|
| 114 | +### With Custom Event Types |
| 115 | +
|
| 116 | +```cpp |
| 117 | +httplib::sse::SSEClient sse(cli, "/events"); |
| 118 | +
|
| 119 | +sse.on_event("notification", [](const httplib::sse::SSEMessage &msg){ |
| 120 | + std::cout << "Notification: " << msg.data << std::endl; |
| 121 | +}); |
| 122 | +
|
| 123 | +sse.on_event("update", [](const httplib::sse::SSEMessage &msg){ |
| 124 | + std::cout << "Update: " << msg.data << std::endl; |
| 125 | +}); |
| 126 | +
|
| 127 | +sse.start(); |
| 128 | +``` |
| 129 | + |
| 130 | +### Async with Stop |
| 131 | + |
| 132 | +```cpp |
| 133 | +httplib::sse::SSEClient sse(cli, "/events"); |
| 134 | + |
| 135 | +sse.on_message([](const httplib::sse::SSEMessage &msg){ |
| 136 | + std::cout << msg.data << std::endl; |
| 137 | +}); |
| 138 | + |
| 139 | +sse.start_async(); // Returns immediately |
| 140 | + |
| 141 | +// ... do other work ... |
| 142 | + |
| 143 | +sse.stop(); // Stop when done |
| 144 | +``` |
| 145 | +
|
| 146 | +### With Custom Headers (e.g., Authentication) |
| 147 | +
|
| 148 | +```cpp |
| 149 | +httplib::Headers headers ={ |
| 150 | +{"Authorization", "Bearer token123"} |
| 151 | +}; |
| 152 | +
|
| 153 | +httplib::sse::SSEClient sse(cli, "/events", headers); |
| 154 | +sse.start(); |
| 155 | +``` |
| 156 | + |
| 157 | +### Error Handling |
| 158 | + |
| 159 | +```cpp |
| 160 | +sse.on_error([](httplib::Error err){ |
| 161 | +std::cerr << "Error: " << httplib::to_string(err) << std::endl; |
| 162 | +}); |
| 163 | + |
| 164 | +sse.set_reconnect_interval(1000); |
| 165 | +sse.set_max_reconnect_attempts(5); |
| 166 | + |
| 167 | +sse.start(); |
| 168 | +``` |
| 169 | + |
| 170 | +## SSE Protocol |
| 171 | + |
| 172 | +The client parses SSE format according to the [W3C specification](https://html.spec.whatwg.org/multipage/server-sent-events.html): |
| 173 | + |
| 174 | +``` |
| 175 | +event: custom-type |
| 176 | +id: 123 |
| 177 | +data:{"message": "hello"} |
| 178 | +
|
| 179 | +data: simple message |
| 180 | +
|
| 181 | +: this is a comment (ignored) |
| 182 | +``` |
0 commit comments