0Pricing
WebSockets & Realtime Systems Programming · Lezione

Sottoprotocolli, estensioni e compressione

Approfondisca il protocollo WebSocket negoziando sottoprotocolli applicativi e abilitando estensioni come la compressione per messaggio.

Sottoprotocolli, estensioni e compressione è una lezione WebSockets & Realtime Systems Programming gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebSockets & Realtime Systems Programming, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebSockets & Realtime Systems Programming include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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, json

Server 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-ws

Subprotocols 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 chose

What 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-deflate

Per-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-deflate compresses text payloads
  • Both are fixed at handshake time

You now understand the negotiation layer of WebSockets.

Domande Frequenti

La lezione «Sottoprotocolli, estensioni e compressione» è gratuita?

Sì — il testo completo di «Sottoprotocolli, estensioni e compressione» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebSockets & Realtime Systems Programming, passa a CoddyKit PRO. Il corso WebSockets & Realtime Systems Programming include 4 lezioni in totale.

Cosa imparerò in «Sottoprotocolli, estensioni e compressione»?

Approfondisca il protocollo WebSocket negoziando sottoprotocolli applicativi e abilitando estensioni come la compressione per messaggio. Eserciti WebSockets & Realtime Systems Programming con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare WebSockets & Realtime Systems Programming?

Non è richiesta alcuna esperienza precedente. WebSockets & Realtime Systems Programming su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Sottoprotocolli, estensioni e compressione»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione WebSockets & Realtime Systems Programming?

Sì. Ogni lezione WebSockets & Realtime Systems Programming include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Il WebSocket handshake spiegato
  2. Framing e messaggi dei dati WebSocket
  3. Ciclo di vita e stati della connessione
  4. Sottoprotocolli, estensioni e compressione
← Torna a WebSockets & Realtime Systems Programming