Upgrading to a socket
A WebSocket starts as an HTTP request that asks to upgrade the connection. After the handshake, both sides can send messages at any time over one persistent connection. WebSocket itself only frames bytes; it does not define what the messages mean.
Why subprotocols exist
To agree on message meaning, the client lists candidate subprotocols in the Sec WebSocket Protocol header during the handshake. The server picks one and echoes it back. Both sides then speak that agreed application protocol, such as a messaging or pub sub format.
Negotiation rules
- The client offers an ordered list of preferences.
- The server must choose exactly one from that list, or none.
- If the server selects a value the client did not offer, the client should fail the connection.
- Picking none means both sides default to a bare message stream they must coordinate out of band.
The handshake completes with status 101 Switching Protocols. Choosing a named subprotocol lets a single endpoint serve several protocols and version them cleanly over time.
Key idea
WebSocket subprotocols let the client offer application protocols in the handshake and the server choose one, so both sides agree on message meaning over the upgraded connection.