"Server push" Technology for HTTP persistent connections

Source: Internet
Author: User
Tags dojo toolkit

Many applications, such as monitoring, instant communication, and instant quotation systems, need to transmit background changes to the client in real time without the need for the client to refresh and send requests continuously. This article first introduces and compares common "server push" Solutions, focuses on the two "server push" Solutions of comet-using HTTP persistent connections and without the need to install plug-ins in browsers: ajax-based long polling mode; IFRAME-based and htmlfile-based stream mode. Finally, I analyzed some issues that need to be paid attention to when developing a comet application, and how to use the open-source comet framework-pushlet to build my own "server push" application.

Application of "server push" Technology

Traditional web systems work in the form of client requests and server responses. This method cannot meet the needs of many practical applications, such:

  • Monitoring System: Background hardware hot swapping, led, temperature, and voltage changes;
  • Instant Messaging System: other users log on and send messages;
  • Instant quotation system: the background database content changes;

These applications require the server to transmit updated information to the client in real time without sending a request from the client. "Server push" technology has some solutions in practical applications. This article divides these solutions into two categories: one is to install plug-ins on the browser side and transmit information based on the set interface, or remote calling using RMI and CORBA, while the other type does not require the browser to install any plug-ins and is based on HTTP persistent connections.

To push a "server" application to a web application, you must first consider how to receive and process information on a browser with limited functions:

  1. Whether a set of interfaces or remote calls are required for the client to receive and process information. The client presents the HTML page, Java Applet, or flash window to the user. If you use the set interface and remote call, how to modify the HTML display with JavaScript.
  2. The format of the information used for communication between the client and the server, and the error handling mechanism.
  3. Whether the client needs to support different types of browsers, such as IE and Firefox, and whether to support both Windows and Linux platforms.

"Server push" technology based on client interface Sets

Flash xmlsocket

If the web application user accepts that the application can run normally only when the flash player is installed, using the flash xmlsocket is also a feasible solution.

The basis of this solution is:

  1. Flash provides the xmlsocket class.
  2. Close Combination of JavaScript and FLASH: In JavaScript, you can directly call the interfaces provided by the Flash program.

Implementation Method: embed a Flash program using the xmlsocket class in the HTML page. Javascript calls the set interface provided by the Flash program to communicate with the set interface of the server. After receiving information transmitted by the server in XML format, JavaScript can easily control the display of HTML page content.

For how to build a Flash program that serves as a bridge between JavaScript and flash xmlsocket, and how to call the flash interface in Javascript, see aflax (asynchronous flash and XML) the socket demo and socketjs provided by the project.

The close combination of JavaScript and flash greatly enhances the processing capability of the client. From Flash Player v7.0.19, The xmlsocket port must be greater than 1023. The Linux platform also supports the flash xmlsocket solution. However, this solution has the following Disadvantages:

  1. The Flash Player must be installed on the client;
  2. Because xmlsocket does not have the HTTP tunnel function, the xmlsocket class cannot automatically pass through the firewall;
  3. Because an interface is used, you need to set a communication port. Firewall and proxy servers may also restrict non-HTTP Channel ports;

However, this solution has been widely used in some online chat rooms and online interactive games.

Java Applet set Interface

Use the Java Applet on the clientjava.net.SocketOrjava.net.DatagramSocketOrjava.net.MulticastSocketEstablish an interface connection with the server to push the server ".

The biggest disadvantage of this solution is that the Java Applet cannot update the HTML page content through javascript after receiving the information returned by the server.

Server push technology based on HTTP persistent connection

About comet

As the front-end of Web applications, browsers have limited processing capabilities. Browser development requires upgrading the client software. Due to the diversity of client browser software, it also affects the promotion of new browser technologies. In Web applications, the main task of a browser is to send a request and parse the information returned by the server in different styles. Ajax is the result of the development of browser technology. By sending asynchronous requests on the browser side, the responsiveness of single-user operations is improved. However, web is essentially a multi-user system. For any user, the server can be considered another user. The development of existing Ajax technology cannot solve the problem of transmitting updated information to the client in real time in a multi-user web application, so that users may perform operations under "obsolete" information. The Application of Ajax makes it possible to update background data more frequently.

Figure 1. Comparison between traditional Web Application Models and Ajax-based models

 

Ajax-based long-Polling (long-polling)

 

1. 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:

  1. The server blocks requests until data transmission or timeout occurs.
  2. After processing the information returned by the server, the client's JavaScript Response Processing Function sends a request again and establishes a new connection.
  3. 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.

Figure 2. server push model based on long polling

 

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.


"Server push" is a technology that exists for a long time. In the past, it mainly used client interfaces or Server Remote calls. 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.

 

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.