Overview -------- "SSEs are sent over traditional HTTP. That means they do not require a special protocol or server implementation to get working. WebSockets on the other hand, require full-duplex connections and new Web Socket servers to handle the protocol. In addition, Server-Sent Events have a variety of features that WebSockets lack by design such as automatic reconnection, event IDs, and the ability to send arbitrary events." -- [html5rocks](https://www.html5rocks.com/en/tutorials/eventsource/basics/) Example ------- ```C++ #include #include #include #include #include #include using namespace std; using namespace restbed; using namespace std::chrono; vector< shared_ptr< Session > > sessions; void register_event_source_handler( const shared_ptr< Session > session ) { const auto headers = multimap< string, string > { { "Connection", "keep-alive" }, { "Cache-Control", "no-cache" }, { "Content-Type", "text/event-stream" }, { "Access-Control-Allow-Origin", "*" } //Only required for demo purposes. }; session->yield( OK, headers, [ ]( const shared_ptr< Session > session ) { sessions.push_back( session ); } ); } void event_stream_handler( void ) { static size_t counter = 0; const auto message = "data: event " + to_string( counter ) + "\n\n"; sessions.erase( std::remove_if(sessions.begin(), sessions.end(), [](const shared_ptr &a) { return a->is_closed(); }), sessions.end()); for ( auto session : sessions ) { session->yield( message ); } counter++; } int main( const int, const char** ) { auto resource = make_shared< Resource >( ); resource->set_path( "/stream" ); resource->set_method_handler( "GET", register_event_source_handler ); auto settings = make_shared< Settings >( ); settings->set_port( 1984 ); auto service = make_shared< Service >( ); service->publish( resource ); service->schedule( event_stream_handler, seconds( 2 ) ); service->start( settings ); return EXIT_SUCCESS; } ``` Build ----- > $ clang++ -o example example.cpp -l restbed Execution --------- > $ ./example Client ------ ```HTML

Incoming Events

```