0Pricing
WebSockets & Realtime Systems Programming · 课时

消息确认与传递保证

通过添加确认、重试和幂等性,确保消息不会悄无声息地丢失,实现可靠的传递保证。

消息确认与传递保证 是 CoddyKit 上的免费 WebSockets & Realtime Systems Programming 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.

常见问题解答

「消息确认与传递保证」课时是免费的吗?

是的 — 「消息确认与传递保证」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebSockets & Realtime Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。

「消息确认与传递保证」这节课中我会学到什么?

通过添加确认、重试和幂等性,确保消息不会悄无声息地丢失,实现可靠的传递保证。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Realtime Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebSockets & Realtime Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebSockets & Realtime Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「消息确认与传递保证」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebSockets & Realtime Systems Programming 课中编写并运行代码吗?

能。每节 WebSockets & Realtime Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 处理断开连接与重新连接
  2. 可靠的错误传播与恢复
  3. 心跳与连接保活
  4. 消息确认与传递保证
← 返回 WebSockets & Realtime Systems Programming