Server-Sent Events in C++: Real-time Without Magic and Extra Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why a regular HTTP request is like a letter by mail, and SSE is a real-time telegram

\n\n

Imagine: 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\n

Now 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\n

In 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\n

How do Server-Sent Events work? Protocol breakdown

\n\n

SSE 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.

\n\n

The message format is extremely simple:

\n\n
data: This is the event text\\n\\n
\n\n

Each 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.

\n\n

Example of a full stream:

\n\n
id: 42\nevent: new_message\ndata: {"user": "Vasily", "text": "Hello!"}\n\nid: 43\nevent: ping\ndata: Server is alive\n\n
\n\n

The client (browser or any application) processes these lines and generates JavaScript events. Everything ingenious is simple.

\n\n

SSE vs WebSocket: when to choose what?

\n\n

Many 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
\n\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\n

Writing an SSE server in C++ from scratch

\n\n

For 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\n

Our server will:

\n
    \n
  • Accept an HTTP request from the client
  • \n
  • Determine that the client wants SSE (by the Accept: text/event-stream header)
  • \n
  • Send a stream of events with the current time every 2 seconds
  • \n
\n\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                

Blogs

Book Recommendations