การรับรองข้อความและการรับประกันการส่ง
ป้องกันไม่ให้ข้อความสูญหายโดยไม่แจ้งเตือนด้วยการเพิ่มการรับรอง การลองใหม่ และการทำงานซ้ำได้อย่างปลอดภัย เพื่อให้การส่งมีความน่าเชื่อถือ
การรับรองข้อความและการรับประกันการส่ง เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Realtime Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Messages Can Vanish
A message can be lost if the socket drops mid-send, the server crashes, or the client reloads before processing. Without acknowledgements, neither side knows it happened.
Delivery Guarantee Levels
Three common guarantees:
- At-most-once: may lose, never duplicate
- At-least-once: never lose, may duplicate
- Exactly-once: never lose, never duplicate (hardest)
Application-Level Acks
WebSockets only confirm transport delivery, not processing. To know a message was handled, the receiver must send an explicit ack back.
Adding a Message Id
Tag each message with a unique id so acks can reference it.
const msg = { id: crypto.randomUUID(), type: 'order', data };
ws.send(JSON.stringify(msg));Sending the Ack
The receiver replies with an ack carrying the original id once it has safely processed the message.
ws.send(JSON.stringify({ type: 'ack', id: msg.id }));Tracking Unacked Messages
The sender keeps a map of in-flight messages until their ack arrives.
const pending = new Map();
pending.set(msg.id, msg);Retrying on Timeout
If no ack arrives within a timeout, resend the message. This is how at-least-once delivery is achieved.
setTimeout(() => {
if (pending.has(msg.id)) ws.send(JSON.stringify(msg));
}, 3000);The Duplicate Problem
Retries can deliver the same message twice (e.g. the ack was lost, not the message). At-least-once therefore implies the receiver may see duplicates.
Idempotency to the Rescue
Make processing idempotent: handling the same id twice has the same effect as once. Track seen ids and ignore repeats.
const seen = new Set();
if (seen.has(msg.id)) return;
seen.add(msg.id);
process(msg);Approaching Exactly-Once
True exactly-once is impossible to guarantee end-to-end, but at-least-once delivery plus idempotent handling gives the same practical result, which is what most systems use.
Best Practices
Build reliable delivery:
- Tag messages with unique ids
- Require application-level acks
- Retry unacked messages with a timeout
- Make processing idempotent to absorb duplicates
Quick Check
Test your delivery knowledge.
Recap
You added delivery guarantees to realtime messaging:
- Understand at-most/at-least/exactly-once
- Use message ids and application-level acks
- Retry unacked messages
- Combine at-least-once with idempotency
Your system no longer silently loses messages.
เรียนรู้ WebSockets & Realtime Systems Programming ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 47
คำถามที่พบบ่อย
บทเรียน “การรับรองข้อความและการรับประกันการส่ง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การรับรองข้อความและการรับประกันการส่ง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Realtime Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การรับรองข้อความและการรับประกันการส่ง”
ป้องกันไม่ให้ข้อความสูญหายโดยไม่แจ้งเตือนด้วยการเพิ่มการรับรอง การลองใหม่ และการทำงานซ้ำได้อย่างปลอดภัย เพื่อให้การส่งมีความน่าเชื่อถือ คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Realtime Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Realtime Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การรับรองข้อความและการรับประกันการส่ง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม
ได้ บทเรียน WebSockets & Realtime Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดการการตัดการเชื่อมต่อและการเชื่อมต่อใหม่
- การส่งต่อและกู้คืนข้อผิดพลาดอย่างมีประสิทธิภาพ
- สัญญาณชีพและการคงการเชื่อมต่อ
- การรับรองข้อความและการรับประกันการส่ง