WebSocket
What it is for
★★★ HTTP is directionally asymmetric: the client asks and the server answers. If the server wants to push a message to the browser, it simply cannot.
★★★ WebSocket exists to fix exactly that: it handshakes with one HTTP request and, once it has a 101, converts that TCP connection into a two-way pipe where either end may send at any time.
★★★ How it relates to HTTP
★★★★ It is HTTP only for the handshake; after that not one HTTP byte remains.
· It borrows an HTTP handshake in order to get through: reusing 80/443 and passing firewalls and proxies;
· After the 101 the frame format is entirely its own, with a header of just 2 to 14 bytes;
· So it is not a fourth generation of HTTP -- 1.1, 2 and 3 are all still request-response, and this is not.
★★ The grey stack in the diagram matches HTTP/2's, but everything inside it has changed.
★ Common misconceptions
★★★ "WebSocket is a kind of HTTP" -- only the handshake packet is. After that even the framing differs.
★★★ "Use it for everything now" -- it has no caching, no status codes and cannot be accelerated by a CDN. Ordinary data fetching still belongs to HTTP.
★★ "Plain ws:// is fine" -- proxies frequently break it, so production uses wss:// (that is, inside TLS) without exception.