"Instant messaging technology analysis and practical" study notes 3 - IM system how to ensure real-time messages

IM technology experienced several iterative upgrade, as shown:
IM history of the evolution of technology

From simple, short polling inefficient escalating to a controllable length relative efficiency of the poll;
full duplex Websocket solve the problem of the push server; and
IM-based TCP protocol derived long connection can be realized active push the server.

A short and long polling-based protocol HTTP connection

Short polling
Long connection
Scenes Periodically polling service new messages to the high frequency side. When the server receives the request, if a new message is a new message will be returned to the client, no news returns an empty list, and closes the connection. That is: there is no server regardless of the current round of news generated, will immediately respond and return. When this request is not acquired a new message, the result will not immediately return the response, but the server "suspension (Hang)" request and waiting for a period of time, once a new message is generated during this time, and can immediately return a response.
Shortcoming For the client, the higher the frequency of the polling, the majority of requests are useless and expensive electricity (cost effectiveness) and cost flow (net cost); on the service side, the high frequency of requests will result in higher QPS and cause greater pressure on the back-end storage resources. Suspension live server request only the reduced inlet QPS request and does not reduce the pressure on the rear end of polling resources. (If there are 1000 requests waiting message, you might have 1000 threads continually polling); do not get long-polling will end returns to the message, still not completely solve the problem the client is invalid requests within the timeout period.

Conclusion: The short and long polling connection can not be done entirely event-based "edge-triggered" because both are based on the HTTP protocol, and HTTP is a stateless protocol, the server has generated a new message, can not be pushed directly to the client, and the client initiates multiple requests, the server will not record the client's status to the server - all requests can only be initiated by the client.

Second, full-duplex communication based on the protocol of a single TCP connection Websocket

Client and server only needs to complete a handshake, it can create long-lasting connection, and at any time, two-way data transmission.
When the server receives a new message, you can Websocket established connection, direct push, really "edge-triggered" to ensure real-time messages.
Advantages:
1. The two-way communication support server-side push, dramatically reducing server polling pressure;
low control overhead 2. The data exchange, reducing network overhead communication between the parties;
3.Web native support, relatively simple.

Third, the long connection based on IM protocols TCP derived

In the IM domain, except Websocket protocol, as well as XMPP, MQTT peer communications protocol, which is a long connection based on a private protocol TCP derived.
These proprietary protocol, when the line is connected to the user on the server to maintain a good relationship between the user equipment and mapping specific TCP connection to connect to the server, and once established long connection, there has been, unless the network is interrupted. In this way, the client can always find the server, the server also through this relationship at any time to find the corresponding online map of the user's client.

Guess you like

Origin www.cnblogs.com/sunshineliulu/p/11450664.html