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).
Previous
How do you implement presence (online/offline) with WebSockets?
Next
How do you handle binary data over WebSockets?
More WebSockets & Real-time Questions
View all →- Intermediate How do you implement a WebSocket server in Node.js using the `ws` library?
- Intermediate What are Socket.IO rooms and namespaces?
- Intermediate How do you handle WebSocket reconnection logic?
- Intermediate What is the difference between Socket.IO and raw WebSockets?
- Intermediate How do you authenticate WebSocket connections?