Ajax, reverse Ajax, and WebSocket concepts

Source: Internet
Author: User

Ajax  

Asynchronous JavaScript and XML (asynchronous JavaScript and Xml,ajax), a browser feature that can be accessed via JavaScript, It allows the script to send an HTTP request to the behind-the-scenes web site without reloading the page. Ajax has been around for more than a decade, and although its name contains XML, you can almost send anything in an AJAX request, the most commonly used data is JSON, which is close to JavaScript syntax and consumes less bandwidth. Listing 1 shows an example of an AJAX request to retrieve the name of a place by its zip code.

Listing 1. Ajax Request Examples

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 (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 the reverse Ajax can be used in some specific way to simulate an AJAX request, and the server can send events (low latency traffic) to the client as quickly as possible.

The purpose of reverse Ajax is to allow server-side push of information to clients. The AJAX request is stateless by default and can only be requested from the client to the server side. You can bypass this limitation by using technology to simulate reactive communication between the server side and the client.

TTP polling and Jsonp polling

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

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 poll to get the two server-side events received.

Figure 1. Reverse Ajax using HTTP polling

Jsonp polling is basically the same as HTTP polling, but the difference is that JSONP can issue cross-domain requests (not requests within your domain). Listing 1 uses JSONP to get a place name by postal code, and JSONP requests are usually identified by its callback parameters and return content, which are executable JavaScript code.

To implement polling in JavaScript, you can use SetInterval to make periodic AJAX requests, as shown in Listing 2:

Listing 2. JavaScript polling

SetInterval (function () {
$.getjson (' Events ', function (events) {
Console.log (events);
});
}, 2000);

 

Advantages and disadvantages of polling implemented with JavaScript:

1. Advantages: It is easy to implement, does not require any server-side specific features, and can work on all browsers.

2. Cons: This method is rarely used because it is completely non-scalable. Imagine the loss of bandwidth and the amount of resources in cases where 100 clients each issued a 2-second polling request, in which case 30% of the requests did not return data.

Piggyback

A piggyback poll (piggyback polling) is a smarter approach than polling because it removes all non-essential requests (those that do not return data). There is no time interval, and the client sends requests to the server when needed. The difference is that in the part of the response, the response is divided into two parts: the response to the request data and the response to the server event, if any part of it occurs. (Is it not similar to submitting a request for detection before the parameter is not requested?) )

You can see the results of the form validation and the events attached to the response, as well as the pros and cons of this approach:

1. Pros: There is no request to return data because the client controls when the request is sent and consumes less resources. This method is also available on all browsers and does not require special features on the server side.

2. Cons: When events that accumulate on the server side need to be routed to the client, you do not know at all, because this requires a client behavior to request them.

Comet

Reverse Ajax using polling or piggyback is very limited: it is not scalable and does not provide low-latency communication (as soon as the event arrives at the server side, they reach the browser at the fastest speed possible). Comet is a Web application model in which requests are sent to the server side and maintain a long lifetime until a timeout or server-side event occurs. After the request is complete, another long-lived Ajax request is sent to wait for another server-side event. With comet, the Web server can send data to the client without explicit requests.

One of the great advantages of comet is that each client always has a communication link open to the server side. The server side can push events to the client by committing (completing) the response as soon as the event arrives, or it can even accumulate and then continue sending. Because requests remain open for long periods of time, the server side needs special features to handle all these long-lived requests.

WebSocket

WebSocket technology comes from HTML5, a recently emerging technology that many browsers already support (Firefox, Google Chrome, Safari, and so on). WebSocket enables bidirectional, full-duplex communication channels, which open the connection through some kind of HTTP request called the WebSocket handshake, and use some special headers. 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.

Reference Blog: http://kb.cnblogs.com/page/112185/

Ajax, reverse Ajax, and WebSocket concepts

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.