Ensiklopedia VibeKoding: Principles of Real-Time Communication: Polling, SSE, and WebSocket.Ensiklopedia VibeKoding: Principles of Real-Time Communication: Polling, SSE, and WebSocket.
How does the browser achieve real-time data updates? The traditional HTTP protocol is based on a "request-response" model, where the client must proactively initiate a request before the server can return data. If we need to implement real-time scenarios such as chat rooms or stock market feeds, this model faces significant challenges. This chapter introduces three main technologies the frontend uses for real-time data communication: Short Polling, Server-Sent Events (SSE), and full-duplex WebSocket, and explores their principles and applicable scenarios.How does the browser achieve real-time data updates? The traditional HTTP protocol is based on a "request-response" model, where the client must proactively initiate a request before the server can return data. If we need to implement real-time scenarios such as chat rooms or stock market feeds, this model faces significant challenges. This chapter introduces three main technologies the frontend uses for real-time data communication: Short Polling, Server-Sent Events (SSE), and full-duplex WebSocket, and explores their principles and applicable scenarios.
------
The HTTP protocol was originally designed for document retrieval. It has the characteristics of being stateless and unidirectionally initiated by the client:The HTTP protocol was originally designed for document retrieval. It has the characteristics of being stateless and unidirectionally initiated by the client:
In this model, the server cannot proactively notify waiting clients of state changes at any time. To obtain the latest data, other technical architecture solutions must be sought.In this model, the server cannot proactively notify waiting clients of state changes at any time. To obtain the latest data, other technical architecture solutions must be sought.
------
The most straightforward solution is short polling. The client uses a timer (such as setInterval) to automatically send an HTTP request to the server at fixed intervals, asking whether new data has arrived.The most straightforward solution is short polling. The client uses a timer (such as setInterval) to automatically send an HTTP request to the server at fixed intervals, asking whether new data has arrived.
Technical Characteristics and Limitations:Technical Characteristics and Limitations:
------
To reduce the overhead of frequently establishing HTTP connections, Server-Sent Events (SSE) provides a lightweight unidirectional data stream push architecture.To reduce the overhead of frequently establishing HTTP connections, Server-Sent Events (SSE) provides a lightweight unidirectional data stream push architecture.
SSE is built on top of the HTTP protocol. After the client initiates an HTTP request with a special header (Accept: text/event-stream), the server keeps the underlying TCP connection open when returning the response. Subsequently, the server can continuously push text-formatted data to the client through this persistent channel.SSE is built on top of the HTTP protocol. After the client initiates an HTTP request with a special header (Accept: text/event-stream), the server keeps the underlying TCP connection open when returning the response. Subsequently, the server can continuously push text-formatted data to the client through this persistent channel.
Technical Characteristics and Limitations:Technical Characteristics and Limitations:
------
When application scenarios involve high-frequency bidirectional interaction (such as multiplayer online action games, precision collaborative document editing), we need a technology that both reduces communication overhead and enables true duplex communication โ WebSocket.When application scenarios involve high-frequency bidirectional interaction (such as multiplayer online action games, precision collaborative document editing), we need a technology that both reduces communication overhead and enables true duplex communication โ WebSocket.
WebSocket is an independent network communication protocol. It elegantly leverages the HTTP protocol to complete the initial connection:WebSocket is an independent network communication protocol. It elegantly leverages the HTTP protocol to complete the initial connection:
Upgrade: websocket header).Handshake Phase: The client sends a special HTTP request declaring the intention to upgrade to a new protocol (carrying the Upgrade: websocket header).101 Switching Protocols status code.Connection Transformation: If the server supports and agrees to the protocol, it responds with a 101 Switching Protocols status code.Technical Characteristics and Limitations:Technical Characteristics and Limitations:
------
| Dimension | Short Polling | Server-Sent Events (SSE) | WebSocket |
|---|---|---|---|
| Communication Direction | Client proactively polls and pulls (unidirectional) | Server continuously pushes proactively (unidirectional) | Client and server have equal send/receive rights (bidirectional full-duplex) |
| Underlying Protocol | Standard HTTP | Standard HTTP | Independent WebSocket protocol (based on TCP) |
| Data Overhead | Extremely high (includes complete HTTP headers) | Low | Extremely low (minimal data frame headers) |
| Typical Use Cases | Periodically checking completion status of backend async tasks | LLM dialogue unidirectional stream output, news or system notification push | Real-time audio/video signaling, multiplayer online gaming, collaborative whiteboard and editing |
In actual engineering, developers should balance system maintenance complexity and communication efficiency based on the specific business scenario's requirements for real-time performance and bidirectional interaction frequency, choosing the most fitting technology stack.In actual engineering, developers should balance system maintenance complexity and communication efficiency based on the specific business scenario's requirements for real-time performance and bidirectional interaction frequency, choosing the most fitting technology stack.