HTTP Long polling & short polling

Source: Internet
Author: User
Tags http request
HTTP protocol Introduction:

The HTTP protocol is a request/response paradigm, and each HTTP response is generated by a corresponding HTTP request; The HTTP protocol is stateless and is not related to multiple HTTP requests. http Long connection:

The current HTTP protocol is commonly used in the 1.1 version, there is a 1.0 version, the difference between the two is that 1.1 support HTTP long connection, or a persistent connection. 1.0 does not support HTTP long connections, and each time an HTTP request responds, the TCP connection is turned off, and the next The HTTP request will re-establish the TCP connection.

The so-called HTTP long connection, that is, multiple HTTP requests sharing a TCP connection; This reduces the time consumed by multiple proximity to HTTP requests resulting in TCP establishment shutdown. In HTTP 1.1, the connection field in the request header and the corresponding header identifies whether an HTTP long connection, connection:keep-alive, indicates an HTTP long connection; Connection:closed, indicating that the server is shutting down the TCP connection

One of the fields corresponding to the connection is keep-live, which appears in the HTTP response header, and his format is timeout=30, max=5, timeout is two times HTTP request retention time (s), and Max is this TCP connection up to a few HTTP request Reuse http long polling:

HTTP long polling is the server received the request, if there is data, immediately respond to the request; If there is no data will be hold for a period of time, if there is data immediately in response to the request; If the time is not yet available, then respond to the HTTP request, and the browser will post an HTTP response to send a similar HTTP request query for data;

Limitations of HTTP Long polling: The browser-side has the maximum limit on simultaneous HTTP connections to the consolidated server, preferably one long poll for the same user; Server-side no data hold live connection will cause waste, easy to create server bottlenecks; http Short Polling:

HTTP-side polling is the server receives requests regardless of whether any data are directly responsive to HTTP requests; The browser receives HTTP responses over time to send the same HTTP request query for data;

The limitation of HTTP short polling is low real time;

The two are the same point:
You can see that both the HTTP long polling and the HTTP short polling are hold for some time;

Two different points
The interval occurs on the server side or the browser side: HTTP long polling is hold for some time at the server, and HTTP short polling is "hold" for some time on the browser side; Application:

Long polling for the general use of Web IM, im real-time requirements, HTTP long polling control is always on the server side, and the data is on the server side, so real-time high;
such as Sina Meager IM, friend network IM and WEBQQ are all using HTTP long polling to achieve;
The Nodejs asynchronous mechanism looks good to handle the server bottleneck caused by HTTP long polling, which remains to be studied.

HTTP short polling is generally used in places where real-time requirements are not high, such as Sina's meager unread number of queries is the browser side every time to query. Other:

About HTTP long connection a misunderstanding is that the server actively pushes the data, which cannot be implemented under the HTTP protocol, because the HTTP request/response paradigm determines that the server return data in  http must have a browser-side request corresponding to the server, which cannot be pushed to the browser number According to.
regardless of whether HTTP long polling or HTTP short polling guarantees that the same user has only one timed query under multiple tabs, this can be done by caching data in the browser-side cache,,  the data in the browser-side after the HTTP response, and setting an expiration date, and then sending each When HTTP requests are checked for valid data, no send request gets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.