Several methods of front end real-time communication and their advantages and disadvantages

1. Short polling
The principle of short polling is very simple. Every time the client sends a request to obtain the latest data from the server, it simulates the realization of instant messaging to a certain extent.
Advantages: strong compatibility, very simple to implement.
Disadvantages: high latency, most of the requests are useless, consume bandwidth and server resources, and affect performance.

2. Comet
comet has two main implementation methods, one is the long-polling method based on AJAX, and the other is the streaming method based on Iframe and htmlfile, which is usually called a long connection.
Please refer to Comet technology to explain the specific two methods of operation: Web-side real-time communication technology based on HTTP long connection

Long polling: The client sends an Ajax request to the server. The server holds the connection after receiving the request. It returns response information and closes the connection until there is a new message. The client sends a new request to the server after processing the response information.
Advantages: Good compatibility, infrequent requests when there is no message, less resource waste.
Disadvantages: Server hold connection will consume resources, the return data sequence is not guaranteed, and it is difficult to manage and maintain
applications: webQQ, Kaixin.com, School, Hi Web version, Facebook IM, etc.

Long connection: Embed a hidden iframe in the page, set the src attribute of this hidden iframe to a request for a long connection or use an xhr request, and the server will continuously input data to the client.
Advantages: Good compatibility, messages arrive instantly, and no useless requests are sent.
Disadvantages: Server maintains long connections and consumes resources.
Example: Gmail chat

3. For the
SSE usage guide, please see the Server-Sent Events tutorial SSE (Server-Sent Event, server push event) is a HTML5 technology
that allows the server to push new data to the client. Advantages: It is based on HTTP, so there is no need for too much Multiple modifications can be used, easy to use, and websocket is very complicated, you must rely on mature libraries or frameworks.
Disadvantages: text-based transmission efficiency is not as high as websocket, not strict two-way communication, and the client cannot reuse the previous connection when sending a request to the server. , A separate request needs to be reissued

4.
Websocket Websocket is a brand-new and independent protocol, based on TCP protocol, compatible with http protocol, but will not be integrated into http protocol, only as part of html5, its role is to establish real-time two-way communication between server and client .
Advantages: real real-time two-way communication, good performance, low latency.
Disadvantages: independent of the http protocol, so additional project transformation is required, high complexity in use, mature libraries must be introduced, and low version browsers cannot be compatible

5.
Web Worker The role of Web Worker is to create a multi-threaded environment for JavaScript, allowing the main thread to create Worker threads and assigning some tasks to the latter to run

6.
Service workers Service workers essentially act as a proxy server between the web application and the browser, and can also act as a proxy between the browser and the network when the network is available, creating an effective offline experience.

7. Flash Socket: Embed a Flash program using the Socket class in the page. JavaScript communicates with the Socket interface of the server by calling the Socket interface provided by the Flash program. JavaScript controls the display of the page after receiving the information transmitted by the server. . Generally used in online games, the web is basically not applicable, and as early as July 2017, Flash's natal Adobe has announced that it will end Flash support at the end of 2020. All browsers will also end their support for Flash around the end of 2020.
Advantages: Realize real instant messaging, not pseudo instant messaging.
Disadvantages: The client must install the Flash plug-in; it is not an HTTP protocol and cannot automatically traverse the firewall.
Example: online interactive games.

Guess you like

Origin blog.csdn.net/weixin_43236062/article/details/107756103