WebSockets

Aliases
  • WebSocket
Image of Author
August 31, 2024 (last updated September 1, 2024)

https://en.wikipedia.org/wiki/WebSocket

WebSocket is a computer communications protocol, providing a simultaneous two-way communication channel over a single Transmission Control Protocol (TCP) connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011.

https://datatracker.ietf.org/doc/html/rfc6455

WebSockets begin as an HTTP upgrade request, which initiates the handshake protocol, which responds to said upgrade request with a 101 Switching Protocols HTTP response. The final step is the websocket connection is finally established and binary frames are passed between the client and server in a full-duplex manner. (I'm not confident on how literally this connection is established, just that it is initiated over TCP after the 101 response.)

Browsers have protocol support for websockets via URLs that start with ws:// and wss:// (which stand for "websocket" and "websocket secure" respectively). It is still the case the websocket connection is established via the HTTP upgrade mechanism, it is just that the browser handles the handshake implicitly.

In my opinion it is curious that websocket connections require an HTTP connection initially. I assume there are good historical reasons for this, but in principal it seems odd because HTTP and WebSockets are essentially sibling protocols, both sitting on top of TCP. Presumably there could be a way to establish a WebSocket connection directly from TCP, similar to how it is done with HTTP. This would make it feel more like a true sibling protocol. I could imagine a world where the evolution of the internet is such that the client/server model falls out of favor and full-duplex connections become the main communication paradigm. In such a world this HTTP to WebSocket connection would feel even more odd.