Server-Sent Events (SSE) is a technology that allows a server to continuously push events to a client over an HTTP connection.
Introduction
SSE approach cleverly transforms HTTP’s traditional request-response pattern into a continuous data stream without requiring protocol changes or additional infrastructure.
The mechanism is deceptively simple but profoundly effective:
-
One-way communication channel: Unlike WebSockets, which establish a bidirectional channel, SSE creates a unidirectional channel from server to client.
-
Chunked Transfer Encoding: Server-Sent Events (SSE) rely on the ability to keep an HTTP connection open indefinitely and to send data in incremental chunks. HTTP/1.1 introduced features such as persistent connections and, most importantly, chunked transfer encoding, which allow the server to stream data piece (chunk) by piece (parts of the response) without knowing their content length (size) in advance. In contrast, HTTP/1.0 lacks native support for these streaming features. An HTTP/1.0 connection expects a fixed Content-Length or a connection close to signal the end of the data, making it unsuitable for continuously sending updates as required by SSE.
-
Event boundaries: The SSE protocol defines clear event boundaries using a simple text-based format with double newlines (
\n\n), making parsing straightforward.
The server never officially terminates the HTTP response. Instead, it maintains an open connection, continuously sending data chunks that follow a specific format. The client, equipped with the EventSource API, intelligently parses these chunks as individual events, creating the illusion of real-time push notifications while remaining firm within the HTTP paradigm.
For example, when using ChatGPT, when you ask it a question, you will see it displaying the answer word by word. In reality, ChatGPT proactively ‘pushes’ the pre-computed data to you, using SSE technology to return data while calculating, thus avoiding long interface waiting times that could lead to direct page closure.
