HTTP Long polling & short polling

Source: Internet
Author: User
HTTP protocol Description:

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 there is no relationship between multiple HTTP requests.

HTTP Long connection:

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

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

A field corresponding to connection is keep-live, the HTTP response header appears, his format is timeout=30, max=5, timeout is two times the HTTP request is kept (s), and Max is this TCP connection up to several HTTP request Reuse

HTTP Long polling:

HTTP long polling is when the server receives the request, if there is data, respond to the request immediately; If there is no data will hold a period of time, if there is data in response to the request immediately; If the time is up and there is no data, respond to the HTTP request, and the browser receives an HTTP response after sending an identical HTTP request query for data;

Limitations of HTTP Long polling:

    1. The browser side has the biggest limit to the unified server HTTP connection at the same time, preferably only one long polling for the same user;
    2. Server-side no data hold the connection will cause waste, prone to server bottlenecks;

HTTP Short Polling:

HTTP side polling is the server receiving requests whether or not there is data that responds directly to HTTP requests; The browser is subjected to HTTP response for a period of time to send the same HTTP request query whether there is data;

The limitation of HTTP short polling is low real-time;

The two are the same point:
You can see that HTTP long polling and HTTP short polling will hold for a period of time;

They are different.
Interval occurs on the server or browser side: HTTP long polling on the server will hold a period of time, HTTP short polling on the browser side "hold" for a period of time;

Application:

Long polling generally used in web IM, im high demand for real-time, HTTP long polling control is always on the server side, and the data is on the server side, so the real-time high;
Like Sina's meager IM, friend net IM and WEBQQ are all implemented with HTTP long polling;
NodeJS's asynchronous mechanism seems to be able to handle the server bottleneck caused by HTTP long polling, which needs to be researched.

HTTP short polling generally used in the real-time requirements are not high, such as Sina's meager unread bar number query is browser-side every time query.

Other:

About HTTP long connection a misunderstanding is that the server actively pushes the data, which is not possible 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 cannot be actively pushed to the number of browsers According to.
Whether HTTP long polling or HTTP short polling guarantees that the same user can only have a single timed query under multi-tab, this is done by caching data in the browser-side, caching the data in the browser after the HTTP response, and setting an expiration date, and then sending the HT TP Request check for valid data, no send request gets

The above describes the HTTP long polling & short polling, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.