Subprotocols, Extensions, and Compression
Go deeper into the WebSocket protocol by negotiating application subprotocols and enabling extensions like per-message compression.
Subprotocols, Extensions, and Compression is a free WebSockets & Realtime Systems Programming lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebSockets & Realtime Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond the Basics
The raw WebSocket protocol only moves bytes. Two negotiation mechanisms let you layer meaning and efficiency on top: subprotocols and extensions.
What Is a Subprotocol
A subprotocol is an agreed application-level message format, like a contract for what the bytes mean. Examples include wamp, mqtt, and graphql-ws.
Requesting a Subprotocol
The client lists desired subprotocols in a handshake header.
Sec-WebSocket-Protocol: graphql-ws, jsonServer Selection
The server picks one subprotocol it supports and echoes it back. If none match, it omits the header and only the base protocol applies.
Sec-WebSocket-Protocol: graphql-wsSubprotocols in the Browser
Pass an array of subprotocol names as the second argument when opening the socket.
const ws = new WebSocket('wss://api.example.com', ['graphql-ws', 'json']);
console.log(ws.protocol); // the one the server choseWhat Is an Extension
An extension changes how frames are transmitted at the protocol level, transparent to your application logic. The most common is per-message compression.
Negotiating Extensions
Extensions are negotiated in the handshake too, via their own header.
Sec-WebSocket-Extensions: permessage-deflatePer-Message Deflate
permessage-deflate compresses each message with DEFLATE before sending. It cuts bandwidth for text-heavy payloads at the cost of CPU.
When Compression Helps
Compression wins for large, repetitive JSON or text. For tiny or already-compressed payloads (images, binary), it adds overhead without benefit and can be disabled.
Subprotocol vs Extension
Keep them straight:
- Subprotocol: meaning of the messages (application layer)
- Extension: how frames are transported (protocol layer)
Practical Notes
Both are negotiated only during the handshake and cannot change afterward. Always verify ws.protocol client-side so your code parses messages with the agreed format.
Quick Check
Test your protocol knowledge.
Recap
You explored advanced WebSocket negotiation:
- Subprotocols agree on message format via
Sec-WebSocket-Protocol - Extensions change frame transport via
Sec-WebSocket-Extensions permessage-deflatecompresses text payloads- Both are fixed at handshake time
You now understand the negotiation layer of WebSockets.
Frequently asked questions
Is the “Subprotocols, Extensions, and Compression” lesson free?
Yes — the full text of “Subprotocols, Extensions, and Compression” is free to read here on the web, and the WebSockets & Realtime Systems Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebSockets & Realtime Systems Programming course, upgrade to CoddyKit PRO.
What will I learn in “Subprotocols, Extensions, and Compression”?
Go deeper into the WebSocket protocol by negotiating application subprotocols and enabling extensions like per-message compression. You practise WebSockets & Realtime Systems Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebSockets & Realtime Systems Programming?
No prior experience is required. WebSockets & Realtime Systems Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Subprotocols, Extensions, and Compression” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebSockets & Realtime Systems Programming lesson?
Yes. Every WebSockets & Realtime Systems Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The WebSocket Handshake Explained
- WebSocket Data Framing and Messages
- Connection Lifecycle and States
- Subprotocols, Extensions, and Compression