What is a WebSocket subprotocol? Agree on the message rules for a connection
A WebSocket subprotocol is a name that identifies the application message rules used over a persistent connection. When opening a connection, the client offers supported options such as chat.v1, and the server selects one in the handshake. This does not change WebSocket itself or define message formats automatically: both programs must implement the same contract for the selected name.
A WebSocket subprotocol is a name that identifies the application message rules used over a persistent connection. When opening a connection, the client offers supported options such as chat.v1, and the server selects one in the handshake. This does not change WebSocket itself or define message formats automatically: both programs must implement the same contract for the selected name.
What does a subprotocol solve?
WebSocket defines connections, frames, and sending text or binary messages, but not the fields or meanings of application messages such as subscribe, message, or error. A subprotocol identifies that application-specific contract. If inventory.v2 is selected, for example, the client and server still need to document its JSON schema, commands, error handling, and version compatibility. Negotiating the name does not make the server validate JSON or translate application versions.
Offer protocol options in a browser
The browser WebSocket constructor accepts a URL and an optional protocol name or list of names. The browser offers them in the handshake's Sec-WebSocket-Protocol header.
const socket = new WebSocket("wss://example.com/realtime", [
"inventory.v2",
"inventory.v1"
]);
socket.addEventListener("open", () => {
console.log("Selected protocol:", socket.protocol);
});This example offers inventory.v2 first and allows inventory.v1 under an application policy if the server does not support v2. socket.protocol contains the successfully negotiated name. Under the current WebSocket standard, if the browser offered a non-empty list and the server selects none or returns a name that was not offered, the connection can fail. Do not infer that the application message contract is correct just because the connection opened; check socket.protocol immediately after opening.
Server selection and version policy
The server selects at most one name from the offered Sec-WebSocket-Protocol values that it actually implements and returns it in the handshake. If no offered value is supported, negotiation cannot complete when the browser client requested protocols, so the connection fails and the client should check its supported versions or configuration. The server must not select a name the client did not offer.
Define the list order as a client preference and document how the server interprets it. Some server frameworks select according to their own supported-list order, so do not assume the client's ordering always wins. When offering multiple compatible versions, document each version's message fields and behavior and verify interoperability.
Keep subprotocols, extensions, and authentication distinct
A subprotocol names an application message contract. An extension such as permessage-deflate negotiates frame-processing behavior. They use different handshake headers and rules. A subprotocol name is not an authentication token or secret and does not authenticate a user. The browser's built-in API does not expose a parameter for arbitrary handshake headers, so choose authentication according to the threat model, such as cookies/sessions or an application authentication message after connection if the server permits it. Long-lived secrets in URL query strings can leak through histories and logs.
Operational checklist
Document each subprotocol's message schema, direction, errors, size limits, and compatibility policy.
Version names consistently; never reuse one name for different contracts.
Have the server select only an implemented offer and return the exact selection in `Sec-WebSocket-Protocol`.
Check `socket.protocol` immediately after opening and close before sending messages if it is unexpected.
Verify both the client's offer order and the server framework's selection preference.
Review extension negotiation and authentication separately; do not grant access based only on the selected subprotocol.
Verify handshake success and mismatch rejection for each supported client/server version pair.
A subprotocol is the negotiation point that connects a WebSocket connection to message rules the application understands. Treating it as a versioned contract helps diagnose handshake failures and evolve compatibility. If the name, implementation, documentation, and server selection policy do not agree, the connection may open yet fail at the message layer.
Sources
[WHATWG WebSockets Standard](https://websockets.spec.whatwg.org/)
[RFC 6455: The WebSocket Protocol, sections 1.9 and 4](https://www.rfc-editor.org/rfc/rfc6455.html)