Polling: The client periodically sends an AJAX request to the server, returning the response information and closing the connection as soon as the server receives the request.
Pros: It's easier to write back-end programs.
Cons: Half of the requests are useless, wasting bandwidth and server resources. (and each HTTP request and response has full HTTP header information, which increases the amount of data that is transmitted each time)
Example: suitable for small applications.
Long polling: The client sends an AJAX request to the server, The server hold the connection after the request is received, until a new message returns the response information and closes the connection (or closes the connection at a set time-out), and the client processes the response information before sending a new request to the server.
Pros: No more frequent requests in the absence of messages, saving network traffic , which solves the dilemma that the service side has been tired of accepting requests
example: Webqq, Hi Web, Facebook IM.
Long connection: Embed a hidden iframe in the page, The src attribute of this hidden IFrame is set to the request of a long connection, and the server can continuously input data to the client.
instance: Gmail chat
Flash Socket: Embed a flash program JavaScript using the socket class within the page to communicate with the server-side socket interface by invoking the socket interface provided by this flash program. JavaScript controls the display of the page after it receives information from the server side.
Pros: Realize real instant communication, not pseudo-instant.
Disadvantage: The client must have a Flash plugin installed, a non-HTTP protocol, and cannot automatically traverse the firewall.
Example: Network interactive game.
WebSocket:
The WebSocket protocol is essentially a TCP-based protocol. In order to establish a WebSocket connection, the client browser first initiates an HTTP request to the server, which differs from the usual HTTP request and contains additional header information, where the additional header information "Upgrade:websocket" indicates that it is an application protocol upgrade The HTTP request, the server side resolves these additional header information and then produces the reply message returns to the client, the client and the server side WebSocket connection is established, the two sides can pass through this connection channel free information, And the connection persists until either the client or the server side actively closes the connection.
Polling, long polling, long connection, socket connection, WebSocket