What is the WebSocket subprotocol?

Answer

The WebSocket subprotocol is an application-level protocol negotiated during the handshake via the Sec-WebSocket-Protocol header. The client lists the protocols it supports (comma-separated), and the server selects one and includes it in the response. The subprotocol defines how messages are structured and interpreted. Examples: STOMP (Simple Text Oriented Messaging Protocol) — used with message brokers like ActiveMQ and RabbitMQ over WebSockets; WAMP (Web Application Messaging Protocol) — provides RPC and Pub/Sub patterns; MQTT over WebSocket — used in IoT applications. If no subprotocol header is sent, both sides must agree out-of-band on the message format (often raw JSON). In Socket.IO's protocol, the subprotocol is socket.io (though it doesn't use standard WebSocket subprotocol negotiation).