WebSocket
A protocol providing a persistent, two-way communication channel between a client and a server.
WebSocket
WebSocket is a communication protocol that establishes a persistent, full-duplex connection between a client and a server over a single TCP connection, allowing either side to send messages to the other at any time without the overhead of repeated HTTP requests. A WebSocket connection begins as a standard HTTP request that is then upgraded to the WebSocket protocol, after which both client and server can send and receive messages independently for as long as the connection stays open.
How it works
Once a WebSocket connection is established, it behaves less like a request and response cycle and more like an open pipe: messages can flow in either direction at any moment, without either side needing to initiate a new request first. This makes WebSocket well suited to interactive, low-latency communication, since there is no need to repeatedly open new connections or poll for updates. The tradeoff is added complexity compared to plain HTTP: WebSocket connections are stateful, need to be explicitly kept alive and reconnected on failure, and require infrastructure such as proxies and load balancers to support connection upgrades correctly.
WebSocket vs Server-Sent Events
Both WebSocket and Server-Sent Events keep a connection open to push data without polling, but WebSocket is bidirectional while SSE only sends data from server to client. A system that only needs to stream output, such as progress updates, generally does not need the added complexity of WebSocket and can use SSE instead. A system that needs true two-way, real-time interaction, such as a live terminal session where a user types input and immediately sees output, needs WebSocket's bidirectional channel.
Why it matters for AI agent systems
Some interactions with an AI agent are not one-way. Debugging an agent, inspecting the environment it is running in, or running commands directly inside its container all require sending input and receiving output on the same connection, in real time. WebSocket is the natural fit for that kind of interactive access, where SSE's one-way streaming would not be enough.
WebSocket in Agenhood
Agenhood provides a WebSocket-based in-browser terminal into a running agent's container, giving an operator direct, interactive shell access alongside the REST API and SSE task-output stream, for cases where inspecting or intervening in an agent's environment directly is necessary.