واجهات WebSocket API للاتصال الفوري
أنشئ اتصالات ثنائية الاتجاه وفورية باستخدام واجهات WebSocket API في API Gateway. تعلّم مسارات الاتصال وقطع الاتصال والرسائل، وكيفية دفع البيانات إلى العملاء.
واجهات WebSocket API للاتصال الفوري درس مجاني في Serverless Backend with AWS Lambda & API Gateway على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Serverless Backend with AWS Lambda & API Gateway، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Serverless Backend with AWS Lambda & API Gateway 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why WebSockets?
REST APIs are request/response — the server cannot push. For chat, live dashboards, or notifications you need a persistent two-way channel. API Gateway WebSocket APIs provide exactly that.
The Three Built-in Routes
A WebSocket API has special routes:
$connectwhen a client connects$disconnectwhen it leaves$defaultfor unmatched messages
Route Selection Expression
Custom routes are chosen by a route selection expression, usually a field in the incoming JSON such as $request.body.action.
{
"action": "sendMessage",
"data": "hello"
}Handling $connect
The $connect handler receives a unique connectionId. Store it (e.g. in DynamoDB) so you can message that client later.
exports.handler = async (event) => {
const id = event.requestContext.connectionId;
await save(id);
return { statusCode: 200 };
};Handling $disconnect
On disconnect, remove the stored connectionId so you do not try to message a dead client.
exports.handler = async (event) => {
await remove(event.requestContext.connectionId);
return { statusCode: 200 };
};Pushing Messages to a Client
To send data back, call the management API with the target connectionId.
const api = new ApiGatewayManagementApi({ endpoint });
await api.postToConnection({
ConnectionId: id,
Data: JSON.stringify({ msg: "hi" })
});Broadcasting to Many Clients
To broadcast, loop over all stored connectionIds and post to each. Remove any that return a 410 Gone status — those clients have disconnected.
Authorizing Connections
Attach a Lambda authorizer to the $connect route to validate a token before the connection is accepted, rejecting unauthorized clients up front.
Connection Lifetime and Limits
WebSocket connections last up to 2 hours, with a 10-minute idle timeout. Clients should reconnect when dropped, and you should handle stale connectionIds gracefully.
Pricing Model
You pay for messages transferred and for connection-minutes, not for idle request count. This makes WebSocket APIs cost-effective for bursty real-time traffic.
When to Use WebSockets
Reach for WebSocket APIs when you need:
- Live chat or collaboration
- Real-time dashboards
- Server-initiated notifications
For simple request/response, stick with REST or HTTP APIs.
Quick Check
Test your WebSocket API knowledge.
Recap
You learned real-time APIs:
- WebSocket APIs add two-way communication
$connect,$disconnect,$defaultplus custom routes- Store connectionIds and push via postToConnection
- Authorize on $connect; handle stale connections
الأسئلة الشائعة
هل درس «واجهات WebSocket API للاتصال الفوري» مجاني؟
نعم — نص درس «واجهات WebSocket API للاتصال الفوري» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Serverless Backend with AWS Lambda & API Gateway، انتقل إلى CoddyKit PRO. تتضمن دورة Serverless Backend with AWS Lambda & API Gateway 4 دروس في المجموع.
ماذا ستتعلم في «واجهات WebSocket API للاتصال الفوري»؟
أنشئ اتصالات ثنائية الاتجاه وفورية باستخدام واجهات WebSocket API في API Gateway. تعلّم مسارات الاتصال وقطع الاتصال والرسائل، وكيفية دفع البيانات إلى العملاء. تتمرن على Serverless Backend with AWS Lambda & API Gateway مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Serverless Backend with AWS Lambda & API Gateway؟
لا تُشترط خبرة سابقة. Serverless Backend with AWS Lambda & API Gateway على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «واجهات WebSocket API للاتصال الفوري»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Serverless Backend with AWS Lambda & API Gateway هذا؟
نعم. كل درس في Serverless Backend with AWS Lambda & API Gateway يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التخزين المؤقت وتقييد المعدل
- تحويلات الطلبات والاستجابات
- أسماء النطاقات المخصّصة وتحسين الحافة
- واجهات WebSocket API للاتصال الفوري