Simple Introduction: one of "server push": Introduction to Comet, simple introduction to comet

Source: Internet
Author: User
Tags dojo toolkit

Simple Introduction: one of "server push": Introduction to Comet, simple introduction to comet

Recently, I had a project where I needed to actively push data to the client from the server. I thought it was very simple, but it was difficult to find in the actual process and it was not as simple as I thought. I learned from online search and found that the mainstream is Ajax long polling technology or stream technology. websocket has more or less some drawbacks. In order to facilitate memory and provide some help and reference to cainiao like me, we will record the articles and experiences learned during the learning process for future inspection and review. Most of the content is organized on the Internet, and some are practical experiences. I hope it will help you.

With the rise of AJAX technology, developers once again saw the opportunity to use browsers to replace desktop applications. AJAX changes the whole page to partial page refresh, and the data is transmitted asynchronously, which causes the visual difference caused by network latency to disappear. AJAX also uses DHTML and a variety of JavasSript languages to simulate various events and response processes of the desktop system, as well as smooth scrolling and drag-and-drop effects. More than that, some IT giants (Google, Sun, Oracle, and so on) provide a wealth of AJAX development tools, making the development and debugging of AJAX applications simple and efficient, in addition, developed AJAX applications can span various browsers and operating systems. In this case, AJAX-based Web applications surge rapidly, consuming the share of the original desktop system. Chat tools, email readers, blog editors, and even Office and word processing software have a beautiful appearance in the browser and almost comparable interactive interfaces with the desktop system. Google also proposed the slogan and Strategy "with browsers and Google, there is no need for Microsoft. In the Ajax world, in addition to traditional CAD design software and large-scale game software, because of the demanding requirements on system hardware, it is also inseparable from desktop systems, it seems that all other applications can become Web applications.

However, there is a fatal defect in AJAX applications in browsers that cannot meet the needs of traditional desktop systems. That is, "Server-Initiated Message Delivery )". In many applications, server software must send messages or messages to clients. Because the server has mastered the main resources of the system, it can first obtain the state changes and events of the system. When these changes occur, the server needs to actively send messages to the client in real time. For example, stock changes. In traditional desktop systems, there is no problem with this requirement because there is usually a persistent connection between the client and the server, and this connection can transmit various data in two directions. However, HTTP-based Web applications do not work. In the Web world, servers always send data passively, provided that the client must first send requests. In fact, the browser does not know when the server information will change. To simulate real-time communication or do not want to miss some information, it can only refresh the page through Polling technology to obtain the latest data. This method is not only a waste of server resources, but the most important thing is that there is a certain delay every time a new HTTP connection is established (or closed). This delay makes applications that frequently transmit information intolerable. As a result, "server push technology" was created ".

"Server push" is a technology that exists for a long time. In the past, it mainly used client interfaces or remote calls on the server. Because browser technology is developing slowly, it does not provide good support for the implementation of server push, in pure browser applications, it is difficult to have a perfect solution to implement "server push" and use it for commercial applications. In recent years, the popularity of AJAX technology and the embedded IFrame in the ActiveX component of "htmlfile" can solve the problem of loading and displaying IE. Some popular applications such as meebo, gmail and gtalk use these new technologies in implementation. At the same time, "server push" has many requirements in real applications. For these reasons, the pure browser-based "server push" technology has attracted a lot of attention, Alex Russell (the Dojo Toolkit project Lead) the "server push" Technology Based on HTTP persistent connections does not require plug-ins to be installed on browsers is "Comet ". Some mature Comet applications and various open-source frameworks have emerged. Some Web servers such as Jetty are also making many improvements to support a large number of concurrent persistent connections. For the latest development of Comet technology, refer to the wiki about Comet.

The following describes the implementation models of the two Comet applications.

I. AJAX-based long-polling

The emergence of AJAX allows JavaScript to call the XMLHttpRequest object to send an HTTP request. The JavaScript Response Processing Function updates the HTML page display based on the information returned by the server. The difference between implementing "server push" using AJAX and traditional AJAX applications is:

The server blocks requests until data transmission or timeout occurs.

After processing the information returned by the server, the client's JavaScript Response Processing Function sends a request again and establishes a new connection.

When the client processes received data and re-establishes a connection, new data may arrive at the server. The information will be saved by the server until the client re-establishes the connection, the client will retrieve all the information on the current server at a time.

Some applications and examples such as "Meebo" and "Pushlet Chat" all adopt this long polling method. Compared with poll, this long polling method can also be called pull ). This method is based on AJAX and has the following advantages: asynchronous request sending; no plug-in installation required; both IE and Mozilla FireFox support AJAX.

In this long polling mode, the client calls the callback function when the readystate of XMLHttpRequest is 4 (that is, the data transmission ends) for information processing. When readystate is 4, the data transmission is complete and the connection is closed. Mozilla Firefox provides Streaming AJAX support, that is, when the readystate is 3 (data is still being transmitted), the client can read data without closing the connection, to read the information returned by the processing server. When the readystate is 3, IE cannot read the data returned by the server. Currently, IE does not support Streaming AJAX.

Ii. streaming Based on Iframe and htmlfile

Iframe is an early HTML Tag. By embedding a hidden frame in an HTML page, you can set the SRC attribute of the hidden frame to a persistent connection request, the server can continuously input data to the client.

The AJAX solution mentioned above is to process the data retrieved from the server by XMLHttpRequest in JavaScript, and then Javascript can easily control the display of HTML pages. The same idea is used on the client of the iframe solution. The iframe server does not return the data directly displayed on the page, but returns the call to the client's Javascript function, for example, "<script type =" text/javascript "> js_func (" data from server ") </script> ". The server transmits the returned data as parameters of the client JavaScript function. the Javascript engine of the client Browser executes the code when receiving the JavaScript call returned by the server.

In this way, the connection will not be closed for each data transmission. The connection will only be closed when communication errors occur or when the connection is rebuilt (Some firewalls are often set to discard too long connections, you can set a timeout time on the server to notify the client to re-establish the connection and close the original connection after the timeout ).

Using iframe to request a persistent connection has an obvious deficiency: The progress bar at the bottom of IE and Morzilla Firefox will show that the loading is not completed, and the icons above IE will keep turning, loading in progress. Google's talents used an ActiveX called "htmlfile" to solve the problem of loading and displaying in IE, and used this method in gmail + gtalk products. Alex Russell: "What else is burried down in the depth's of Google's amazing JavaScript ?" This method is introduced in the article. The comet-iframe.tar.gz provided by the Zeitoun website encapsulates a JavaScript comet object based on iframe and htmlfile. It supports IE and Mozilla Firefox browsers and can be used as a reference.

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.