Agentic Research

WebSocket

Also: 雙向長連線 · 串流 · streaming · SSE · 即時連線

A connection that stays open once established, letting both sides push data at any time — unlike HTTP's ask-once, answer-once, then close.

When you will meet it

Watching an answer appear word by word in ChatGPT is streaming. This is why some tools show output live while others wait and dump it all at once — the difference is a persistent connection versus a single response.

An analogy

HTTP is texting: send one, wait for one, redial each time. A WebSocket is a phone call you leave open: either side speaks whenever, back and forth live — good for anything that keeps updating.

Minimal example

輪詢(polling):用戶端每隔 1 秒問一次「有新資料嗎」→ 多半得到「沒有」,浪費
WebSocket:連線一直開著,伺服器一有新 token 就主動推給用戶端,雙向
SSE:伺服器單向持續推送(建立在 HTTP 上),LLM 的串流輸出多半用這個

The key distinction: for "the server keeps pushing, the client just receives" — like token-by-token LLM output — the mainstream choice is actually SSE (Server-Sent Events), not WebSocket. SSE is simpler and rides plain HTTP. WebSocket is bidirectional, suited to chat rooms and collaborative editing where both sides push live. Both are persistent-connection streaming, unlike a one-shot HTTP response.

What people get wrong

  • Assuming "streaming output = WebSocket". Most LLM APIs stream token-by-token over SSE, which rides plain HTTP and needs only one-way push; WebSocket is a different, heavier bidirectional connection.
  • Faking real-time with polling: asking "done yet?" every second. That wastes requests, trips rate limits sooner, and still feels laggy. For real-time, use a persistent connection (SSE/WebSocket).

Related terms

Next