The labs on this site need JavaScript to run. The same content is written out below.
Learn › Networking › HTTP
Four ways to tell the browser what happened on the server
Why it exists — If the browser asks every few seconds so that it can show news right away, most requests come back empty, the load on the server grows, and notifications are still delayed by up to the length of the interval. SSE and WebSocket keep one connection open and let the server send news the moment it occurs, so it arrives immediately without anyone asking.
Where it's used today — SSE is used where the server only needs to send, such as notification badges, delivery status, progress of a long task, and price quotes, and WebSocket is used where both sides exchange messages often, such as chat, collaborative editing, and online games. In Spring they are built with SseEmitter and WebSocketHandler. If the information changes only once every few minutes, simple polling is enough.
HTTP works by the client asking and the server answering, so even when the server has a new notification, it cannot speak first. The options are to ask periodically (polling), to hold the answer back (long polling), to keep the connection open and stream data through it (SSE), or to switch to a two-way channel (WebSocket).
Checked against real execution · The server and browser code on the screen was run on Spring Boot 4.1.1 (DeferredResult, SseEmitter, TextWebSocketHandler) and Chrome 158, and the request counts, empty responses, order of notification arrival, and open channels were compared for all 72 combinations. Proxies and multi-server setups follow the standards and documentation
// Browser (lastId and show are an example variable and function; their declarations are omitted)
setInterval(async () => {
const res = await fetch("/notifications?after=" + lastId);
show(await res.json()); // an empty array if there is nothing
}, 5000);
Notifications are assumed to occur evenly over the 30-second window. The messages the client sends are fixed at 10 in 30 seconds (one at 0 seconds, then one every 3 seconds). The timeout of long polling is set to 30 seconds. The server code on the screen (waitForNew, stream, and ChatHandler) was compiled as it is and started on Spring Boot 4.1.1 (embedded Tomcat 11.0.24, Spring WebSocket 7.0.9), and the browser code on the screen was run as it is in Chrome 158 on this computer (headless), so all 72 combinations were compared through 36 distinct runs (verify/spring/realtime_probe.out.txt). The number of HTTP requests the browser sent (recorded by the developer tools) and the number the server received, the number of empty responses, the number of notifications each polling request received, and the channel left open at the moment 30 seconds had passed all matched the screen. The only thing changed is the WebSocket address (the ws:// address of this server instead of wss://shop.example/chat). Long polling has no browser code on the screen, so a loop that does what it says ("as soon as it gets a response, it requests again") was added, and the server side of polling, POST /messages, and the example waiters, emitters, broadcast, and show were written separately to run it. The average delivery delay on the screen is calculated from the time a notification occurred and the time of the request that received it. The measured values were 6 to 22ms longer than that for polling, and 0 to 5ms per notification for long polling, SSE, and WebSocket (inside the same computer there is almost no network round trip). Even with polling, the browser did not open a new TCP connection for each request but kept reusing one (HTTP keep-alive). That is why the last column shows not the number of connections but the channel on which the server can send first. Also checked: when the server ended the SSE response, the browser reconnected by itself 3 seconds later, and when the server closed the WebSocket, it did not reconnect for 7 seconds. A long-polling request received an empty array 30 seconds after it was sent. Calling send on the line right after new WebSocket throws InvalidStateError because the connection is not open yet (so the code on the screen sends in onopen). How well these work with proxies and load balancers, and putting Redis publish/subscribe in between when there are several servers, were not run; they follow the HTML standard (EventSource), the WebSocket standard (RFC 6455), and the Spring documentation.
Previous lab: CORS · Next lab: TLS handshake
Struck-through text was removed from the first version of the code; highlighted text was added.