البروتوكولات الفرعية والامتدادات والضغط
تعمّق في بروتوكول WebSocket عبر التفاوض على البروتوكولات الفرعية للتطبيق وتفعيل امتدادات مثل الضغط لكل رسالة.
البروتوكولات الفرعية والامتدادات والضغط درس مجاني في WebSockets & Realtime Systems Programming على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في WebSockets & Realtime Systems Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة WebSockets & Realtime Systems Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.
الأسئلة الشائعة
هل درس «البروتوكولات الفرعية والامتدادات والضغط» مجاني؟
نعم — نص درس «البروتوكولات الفرعية والامتدادات والضغط» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebSockets & Realtime Systems Programming، انتقل إلى CoddyKit PRO. تتضمن دورة WebSockets & Realtime Systems Programming 4 دروس في المجموع.
ماذا ستتعلم في «البروتوكولات الفرعية والامتدادات والضغط»؟
تعمّق في بروتوكول WebSocket عبر التفاوض على البروتوكولات الفرعية للتطبيق وتفعيل امتدادات مثل الضغط لكل رسالة. تتمرن على WebSockets & Realtime Systems Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ WebSockets & Realtime Systems Programming؟
لا تُشترط خبرة سابقة. WebSockets & Realtime Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «البروتوكولات الفرعية والامتدادات والضغط»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس WebSockets & Realtime Systems Programming هذا؟
نعم. كل درس في WebSockets & Realtime Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- شرح المصافحة في WebSocket
- تأطير بيانات WebSocket والرسائل
- دورة حياة الاتصال وحالاته
- البروتوكولات الفرعية والامتدادات والضغط