API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์
สร้างการเชื่อมต่อสองทางแบบเรียลไทม์ด้วย API ของ API Gateway WebSocket เรียนรู้เส้นทางการเชื่อมต่อ การตัดการเชื่อมต่อ และข้อความ รวมถึงวิธีส่งข้อมูลกลับไปยังไคลเอนต์
API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์ เป็นบทเรียน Serverless Backend with AWS Lambda & API Gateway ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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
คำถามที่พบบ่อย
บทเรียน “API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless Backend with AWS Lambda & API Gateway ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์”
สร้างการเชื่อมต่อสองทางแบบเรียลไทม์ด้วย API ของ API Gateway WebSocket เรียนรู้เส้นทางการเชื่อมต่อ การตัดการเชื่อมต่อ และข้อความ รวมถึงวิธีส่งข้อมูลกลับไปยังไคลเอนต์ คุณปฏิบัติ Serverless Backend with AWS Lambda & API Gateway ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless Backend with AWS Lambda & API Gateway หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless Backend with AWS Lambda & API Gateway บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless Backend with AWS Lambda & API Gateway นี้ได้ไหม
ได้ บทเรียน Serverless Backend with AWS Lambda & API Gateway ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การแคชและการจำกัดอัตรา
- การแปลงคำขอและการตอบกลับ
- ชื่อโดเมนแบบกำหนดเองและการเพิ่มประสิทธิภาพที่ขอบเครือข่าย
- API ของ WebSocket สำหรับการสื่อสารแบบเรียลไทม์