Instant messaging solutions
1. Short polling
2. Long polling
3. SSE
4. WebSocket
– short polling
The connection between the client and the server is always ongoing, and inquiries are made once in a while. The front end usually adopts setInterval or setTimeout to continuously request server data
– long polling
> The client sends a request to the server, and if the server has no new data, it holds the connection until it has data. Once the server has data (messages) to send to the client, it uses this connection to send the data to the client. The connection is then closed.
Advantages: comparison polling has been optimized, with better timeliness. Disadvantages: Occupies a large number of memory resources and requests.
– SSE(Sever-Send Event Event pushed by the server)
Strictly speaking, the HTTP protocol does not allow the server to actively push information. A workaround, however, is for the server to declare to the client that the next thing to send isFlow of information
(streaming).
Nguyen other (www.ruanyifeng.com/blog/2017/0.)
That is, instead of sending a one-time packet, a stream of data is sent continuously. Instead of closing the connection, the client waits for a new data stream from the server, as in the case of video playback. In essence, this communication is a stream of information that takes a long time to download.
SSE is a HTML5 technology based on HTTP protocol, which allows the server to push data to the client. Disadvantages: Poor compatibility (IE, Edge does not support); The server can only push one-way data to the client;
– WebSocket
- The HTML5 WebSocket specification defines an API that enables Web pages to communicate bidirectional with remote hosts using the WebSocket protocol. Compared with polling and long polling, it greatly reduces unnecessary network traffic and waiting time.
- WebSocket is an application-layer protocol. It is based on the TCP transport protocol and reuses the HTTP handshake channel. It is not based on HTTP, but rather HTTP before establishing a connection and then upgrading to WS or WSS for the first handshake.
Detailed view: juejin.cn/post/684490…
Advantages: low overhead, two-way communication, support binary transmission.
Disadvantages: the development cost is high, need to do extra reconnection to protect the live.