Why a regular HTTP request is like a letter by mail, and SSE is a real-time telegram
\n\nImagine: you are waiting for a notification about a new comment on a website. You refresh the page every 5 seconds. The server returns a bunch of HTML, CSS, images each time — and only then, at the very bottom, you see: "No new comments." Sound familiar? This is called polling. It works, but it consumes traffic and loads the server.
\n\nNow imagine another scenario: you open a page, and a second later a message appears on it: "Vasily left a comment: 'Great article!'" Without reloading, without extra requests. The server itself "pushes" data to you. This is Server-Sent Events (SSE) — a technology that allows the server to send a stream of events to the client over a regular HTTP connection.
\n\nIn this article, we will break down how to implement SSE in pure C++ without using heavy frameworks. You will learn why Server-Sent Events in C++ is an excellent choice for monitoring, stock quotes, chat notifications, and any tasks requiring one-way real-time data transfer.
\n\nHow do Server-Sent Events work? Protocol breakdown
\n\nSSE is part of the HTML5 specification. The client opens a regular HTTP connection and tells the server: "I am ready to listen for events." In response, the server does not close the connection but starts sending data in a special text/event-stream format.
The message format is extremely simple:
\n\ndata: This is the event text\\n\\n\n\nEach event starts with data: and ends with a double newline. You can add id: for identification, event: for the event type, and retry: to specify the reconnection time in milliseconds.
Example of a full stream:
\n\nid: 42\nevent: new_message\ndata: {"user": "Vasily", "text": "Hello!"}\n\nid: 43\nevent: ping\ndata: Server is alive\n\n\n\nThe client (browser or any application) processes these lines and generates JavaScript events. Everything ingenious is simple.
\n\nSSE vs WebSocket: when to choose what?
\n\nMany immediately think of WebSocket when it comes to real-time. Let's compare:
\n\n- \n
- WebSocket — two-way communication. The client and server can initiate an exchange. Requires a separate protocol (ws://), passing through proxies, often blocked by corporate firewalls. \n
- SSE — one-way communication (server → client). Works over regular HTTP. Automatically reconnects on disconnection. Easily passes through any proxies. \n
If your task is to send notifications to the client, rather than wait for commands from it, Server-Sent Events in C++ is an elegant and simple solution. No need to drag in heavy libraries, no need to write complex handlers.
\n\nWriting an SSE server in C++ from scratch
\n\nFor implementation, we need a minimum: sockets and HTTP handling. In real projects, Boost.Beast or libmicrohttpd are used, but to understand the essence, we will write a simple server in pure C++ using POSIX sockets.
\n\nOur server will:
\n- \n
- Accept an HTTP request from the client \n
- Determine that the client wants SSE (by the
Accept: text/event-streamheader) \n - Send a stream of events with the current time every 2 seconds \n
Step 1: Create a socket and listen on a port
\n\n#include <iostream>\n#include <string>\n#include <cstring>\n#include <unistd.h>\n#include <arpa/inet.h>\n#include <sys/socket.h>\n#include <thread>\n#include <chrono>\n#include <sstream>\n\nvoid handle_client(int client_socket) {\n // Buffer for the request\n char buffer[4096] = {0};\n read(client_socket, buffer, 4096);\n \n // Check that this is a GE