Using Ajax to achieve persistent connections (analog push) (1)

Source: Internet
Author: User

This article is not original. Click to view the original post

Many programs can implement persistent connections through sockets to push messages in real time. However, http requests are usually short connections. Once a request ends, the connection to the server will be closed,
The server cannot actively push data to the client, but can only initiate requests from the client. However, in some cases, real-time message transmission needs to be implemented on the web, such as instant chat and stock dynamic information.
In
Before Ajax technology was discovered, it seemed very difficult to implement a refreshing instant chat on the web page. However, due to the rise of Ajax technology, people can further explore its potential, image
Gmail and google gtalk web pages are all important applications for Ajax persistent connections. Currently, many websites may already use persistent connection technology, such as instant chat on SNS websites.

1. Web chat through Ajax short connections

I drew a model diagram myself:


In the conventional short connection mode, requests are refreshed without interruption. For example, an Ajax request is sent every 3 seconds, and data is updated every 3 seconds. Then, the data is refreshed continuously, until the user closes the network
Page. The disadvantage of this solution is that, if the user does not perform operations for a long time, no new data will be returned for each refresh, which leads to a waste of resources and many requests are unnecessary; another problem is
User input is not synchronized with data requests, which may cause information delay.

2. Web chat through Ajax persistent connections

Model diagram:

 

In the Ajax persistent connection mode, refresh is performed based on data. If new data is returned, The system receives and parses the displayed data and initiates a new Ajax request.
If the user does not operate, the connection is in sleep state, waiting for user input or request timeout, and then initiating the next Ajax request. The advantage of this is that every message is pushed to the client immediately
It is very late. In addition, each request is meaningful. Compared with short connections, the efficiency is much higher. However, for websites with a large number of users and frequent operations, the persistent connection mode will also frequently refresh questions.
Question. Currently, Internet Explorer limits the number of HTTP connections. Each web page can only have two persistent connections at the same time, and the third persistent connection will be blocked.

Using php sleep in the background, I made a Simulated Program to check the effect.

View
Let's look at some cases of Ajax persistent connection chat written by others. Taking php as an example, it basically performs uninterrupted detection in the background. If there is new data, it will be pushed. If there is no new data, until 30 seconds.
Timeout, and then the client initiates the next request again. An important line of code in background judgment: usleep (10000 );
It is to pause for 10 milliseconds to relieve the CPU pressure. I personally feel that if the pause is 100 milliseconds, the time delay is also hard to feel. This method is basically to move the front-end loop to the background, reducing
In the network transmission phase, if it is an Ajax instant chat system that stores user chat records, it needs to be combined with the database for more than 10 data queries per second, this ratio must be added to short connections.
The pressure on the database is greatly increased, and how to optimize it should be handled by the server personnel.

If you want to know more about the content, please Google the comet, or take a look here (a very good framework, Ajax Push Engine). This website has a lot of demos and is very handsome.

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.