我们为什么选择 WebSockets 而非 HTTP 来构建我们的 AI agents

Why we built our AI agents on WebSockets instead of HTTP

When we started building AI Copilots at Liveblocks, we weren’t trying to invent a new protocol. We just needed a way to keep an AI agent in sync with the UI, across tabs, devices, and even after a refresh in the middle of a task.

当我们在 Liveblocks 开始构建 AI Copilots 时,我们并不是试图发明一种新协议。我们只是需要一种方法,让 AI 代理与 UI 保持同步,跨标签页、设备,甚至在任务中间刷新后。

Most AI agents default to HTTP streaming. And that made sense for ChatGPT and first-generation chat UIs. But the more we leaned into UI-first copilots with front-end tool calls, confirmation flows, realtime feedback, and resumable streams, the more things started to break.

大多数 AI 代理默认使用 HTTP 流式传输。对于 ChatGPT 和第一代聊天 UI,这是有道理的。但随着我们更多地依赖 UI 优先的 copilots,带有前端工具调用、确认流程、实时反馈和可恢复流式传输,事情开始崩溃。

So we turned to a solution we had years of experience scaling: a stable and persistent WebSocket stack with authentication, automatic reconnection, and reliable message delivery.

所以我们转向了一个我们有多年扩展经验的解决方案:一个稳定且持久的 WebSocket stack,带有身份验证、自动重连和可靠的消息传递。

This post isn’t prescriptive, and WebSockets aren’t the right answer in every case. But they made our lives much easier in ways we didn’t anticipate. Here’s what we learned.

这篇文章并非规定性的,WebSockets 并非在每种情况下都是正确答案。但它们以我们未预料到的方式让我们的生活变得更容易。这是我们学到的。

HTTP’s request–response model works for basic interactions, but it struggles with long-running processes, page refreshes, or multiple tabs. Once a request ends, the connection is gone. To bridge that gap, teams usually add infrastructure like polling, pub-sub servers, or custom session logic. This becomes especially painful with LLMs, where responses can stream for extended periods and users may join or rejoin mid-process.

HTTP 的请求–响应模型适用于基本交互,但它在长时间运行的进程、页面刷新或多个标签页方面会遇到困难。一旦请求结束,连接就消失了。为了弥补这一差距,团队通常会添加像轮询、pub-sub 服务器或自定义会话逻辑这样的基础设施。对于 LLMs,这变得尤其痛苦,因为响应可能持续流式传输很长时间,用户可能在中途加入或重新加入。

WebSockets provide a persistent, bidirectional connection instead. The server can push updates at any time, and clients can subscribe to an in-progress task and immediately receive the latest state. Additionally, updates broadcast to all connected tab...

开通本站会员,查看完整译文。

Home - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.1. UTC+08:00, 2026-04-14 01:01
浙ICP备14020137号-1 $Map of visitor$