Methods to achieve instant messaging on the Web: There are four main ways to achieve instant messaging, they are polling, long polling (Comet), long connection (SSE), WebSocket. They can be broadly divided into two categories. One is implemented on top of HTTP, including short polling, Comet, and SSE. Another implementation that is not based on HTTP is WebSocket. Here’s a look at each of the four polling methods, along with their pros and cons.

polling

  • Short polling
    • The basic idea is that the browser sends an HTTP request to the browser every once in a while, and the server responds directly to the request, regardless of whether there is any data update. In this way, instant communication is essentially a process in which the browser sends a request and the server accepts the request. By making the client continue to make requests, the client can simulate the change of data received from the server in real time.
    • The advantage of this approach is that it is relatively simple, easy to understand, and there are no technical difficulties in implementing it. The disadvantages are obvious. This approach wastes resources on both the server and the client because of the need to establish HTTP connections constantly. Especially on the client side, in terms of distance, if there are orders of magnitude of people who want to be larger in a short-poll-based application, then every user’s client will frantically send HTTP requests to the server without interruption. The larger the number of people, the greater the pressure on the server side, which is very unreasonable.
  • Long polling (Comet)
    • When the server receives the request from the client,Blocks requests until there is data or a timeoutInstead of responding directly, the server suspends the request and determines whether the server-side data has been updated. If there is an update, it responds, and if there is no data at all, it returns after a certain time limit (set on the server side) is reached. After processing the information returned from the server, the client-side JavaScript response handler makes another request to re-establish the connection.
    • Compared with short polling, long polling significantly reduces the number of unnecessary HTTP requests and saves resources in comparison. The disadvantage of long polling is that hanging connections can also lead to wasted resources.
  • Polling and long polling are both based on HTTP, and both have their drawbacks: short polling requires faster processing; Long polling requires the ability to handle concurrency; Both are examples of a “passive server” : the server does not actively push information, but rather responds when the client sends an Ajax request.

Http1.1 long connection

  • In HTTP1.1, keep-alive is added to keep a long connection with the server, which saves a lot of connection establishment process. However, the communication process is still 1:1, that is, if you want to get data, you must first send a request to get a response. Therefore, in real-time monitoring, push, live video and other scenarios with high real-time performance or demanding bandwidth utilization, it is still not very suitable

Long connection (SSE)Simplex communication

  • SSE is a new feature in HTML5 called Server-sent Events. It allows services to push data to clients. SSE is essentially different from the previous long polling and short polling. Although both are based on HTTP protocol, polling requires the client to send a request first. The biggest feature of SSE is that it does not need the client to send a request. As long as the server data is updated, it can be immediately sent to the client.
  • SSE has obvious advantages. It does not need to establish or maintain a large number of requests sent from the client to the server, saving a lot of resources and improving application performance. And, as we’ll see later, THE IMPLEMENTATION of SSE is very simple and does not rely on other plug-ins.
  • Compared with WebSocket, simplex communication is allowed. After a connection is established, only one connection can be sent from the server to the client. If the client needs to communicate with the server, you need to open an additional connection

WebSocketDuplex communication

  • WebSocket is a new protocol defined by Html5. Different from the traditional HTTP protocol, this protocol can achieve full duplex communication between the server and the client. In simple terms, you first need to establish a connection between the client and server, which requires HTTP. Once the connection is established, the client and server are on equal footing and can send data to each other without distinction between request and response.
  • The advantage of WebSocket is that it realizes two-way communication, but the disadvantage is that the logic on the server side is very complicated. There are now different plug-ins available for different background languages.

Comparison of four Web instant messaging technologies

  • From the point of view of compatibility, short polling > Long polling > long SSE>WebSocket;
  • For performance reasons, WebSocket> long connection SSE> Long polling > short polling.