Video in progress
Can WebSocket run over HTTP/2? What does RFC 8441 change, and will you run into it in practice?
Yes. RFC 8441 lets you open a WebSocket inside an HTTP/2 stream via extended CONNECT: no separate TCP connection is needed, and the handshake ends with 200, not 101. But support in servers and proxies is uneven, and in many places the HTTP/1.1 upgrade is still used.
📖 Answer
Classic WebSocket is tied to the HTTP/1.1 Upgrade mechanism. HTTP/2 has no Upgrade, so even if the browser fetched the page over HTTP/2, it opens a separate HTTP/1.1 TCP connection for WebSocket.
RFC 8441 solves this like so:
:method = CONNECT
:protocol = websocket
:scheme = https
:path = /ws
sec-websocket-version = 13
The server must have sent SETTINGS_ENABLE_CONNECT_PROTOCOL = 1 beforehand. The response is a plain 200, and then the normal WebSocket frames travel inside that stream. There's a similar standard for HTTP/3 as well (RFC 9220).
The benefit is a single TCP/TLS connection: page requests and WebSocket travel together, there's no extra handshake, and the connection opens faster on a mobile network. But everything is inside one TCP connection: if a packet is lost, both the HTTP/2 streams and the WebSocket wait together (head-of-line blocking). Support is uneven too. Browsers know it, but every link in the chain (CDN, load balancer, Nginx, the Java server) has to know it as well. If one link doesn't, the browser quietly falls back to HTTP/1.1.
In practice I don't see this as a design decision. Usually, whatever happens up to the edge, inside the network you go to the backend with an HTTP/1.1 upgrade and the Spring app doesn't notice the difference. The problem only shows up while debugging: people see "Protocol: h2" in DevTools and wonder "where's the WebSocket?".
Sign up to mark studied questions and save your progress.