Reverse Ajax:comet Stream and long polling for response communication between server and client

Source: Internet
Author: User
Keywords Server Html5 ajax comet responding to communications

The client example uses the JQuery http://www.aliyun.com/zixun/aggregation/33906.html ">javascript Library." In this first article, we explore different reverse Ajax technologies, using downloadable examples to learn the Comet of using the streaming (streaming) method and the long polling (polling) method.

WEB Development has made a lot of progress over the past few years, and we've gone way beyond linking static Web pages, which can cause browsers to refresh and wait for the page to load. What is needed now is the ability to access fully dynamic applications via the WEB. These applications usually need to be as fast as possible, providing nearly real-time components. In this 5-part new series, we learn how to use reverse Ajax (Reverse Ajax) technology to develop event-driven WEB applications.

In this first article, we want to learn about reverse Ajax, polling (polling), streaming (streaming), Comet, and long polling (length polling). Learn how to implement different reverse Ajax communication technologies and explore the pros and cons of each approach.

Ajax, reverse Ajax, and WebSockets

Asynchronous JavaScript and XML (Ajax), a feature of browser functionality that can be accessed through JavaScript, allowing scripts to send an HTTP request to a Web site behind the scenes without reloading the page. Ajax has been around for more than a decade, and although its name contains XML, you can almost transmit anything in an AJAX request. The most commonly used data is JSON, which is very close to JavaScript syntax and consumes less bandwidth. Listing 1 in Listing 1 gives an example of an AJAX request to retrieve the name of the place by a zip code somewhere.

Listing 1. Listing 1 examples of Ajax requests

var url = ' http://www.geonames.org/postalCodeLookupJSON?postalcode= ' + $ (' #postalCode '). Val () + ' &country= ' + $ (' # Country '). Val () + ' &callback=? '; $.getjson (URL, function (data) {$ (' #placeName '). Val (Data.postalcodes[0].placename);

Reverse Ajax (Reverse Ajax) is essentially the concept of being able to send data from the server side to the client. In a standard HTTP Ajax request, the data is sent to the server side, and reverse Ajax can simulate an AJAX request in some specific way, which is discussed in this article, so that the server can send events to the client as quickly as possible (low latency traffic).

WebSocket technology comes from HTML5, a technology that has recently emerged and has been supported by many browsers (Firefox, Google Chrome, Safari, etc.). WebSocket enables a two-way, Full-duplex communication channel that opens the connection through some HTTP request called the WebSocket handshake and uses a special header. The connection remains active, and you can use JavaScript to write and receive data just as if you were using an original TCP socket interface. WebSocket in the second part of the article series.

Reverse Ajax Technology

The purpose of reverse Ajax is to have the server push information to the client. Ajax requests are stateless by default, and can only be made from the client to the server side. You can circumvent this limitation by using technology to simulate responsive communication between server-side and client.

HTTP Polling and JSONP polling

Polling (Polling) involves sending a request from the client to the server to get some data, which is clearly a purely Ajax HTTP request. In order to get server-side events as quickly as possible, the polling interval (two times between requests) must be as small as possible. But there is a drawback: if the interval is reduced, the client browser makes more requests, many of which do not return any useful data, which will waste bandwidth and processing resources.

Figure 1 of the timeline in Figure 1 illustrates that some polling requests are made by the client, but no information is returned, and the client must wait until the next polling to get the two server-side events.

Figure 1. Reverse Ajax using HTTP polling

JSONP polling is basically the same as HTTP polling. The difference is that with JSONP you can send Cross-domain requests (requests do not belong to your domain). Listing 1, listing 1, uses JSONP to get a place name from a ZIP code. JSONP requests can usually be identified through its callback parameters and return content, which is executable JavaScript code.

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.