EMZETT.
Login

Server-Sent Events and real time on the web

In short: If the server should send news to the browser (chat, prices, progress bars), there are three ways: polling, server-sent events and WebSockets. Each suits different tasks.

Polling

The browser asks every few seconds. Simple, works everywhere, but wastes requests and is never fully current. Long polling keeps the request open until there is something.

Server-sent events (SSE)

A normal HTTP connection stays open and the server sends events in one direction as lines of text:

const source = new EventSource("/api/events");
source.onmessage = (e) => console.log(JSON.parse(e.data));
source.addEventListener("progress", (e) => update(e.data));

Advantages: very simple, the browser reconnects by itself, runs over normal HTTP. Disadvantage: only server → browser, text. Popular for notifications, live tickers and streaming AI answers.

WebSockets

A permanent two-way connection. Ideal for chats, games and shared editing, where the browser also sends often. More effort (own reconnection, message format) and harder behind some proxies.

Which choice?

NeedSolution
rarely new data, simplicitypolling
server reports newsSSE
both sides send often, small delayWebSocket

On serverless platforms permanent connections are often limited; then you use services with push channels. Basics of transmission: HTTP.

See also: Event Loop