Use ajax to achieve persistent connections (analog push)

Source: Internet
Author: User

This article is not original. ClickViewOriginal post

ManyProgramYou can use a socket to implement persistent connections to push messages in real time. However, HTTP requests are usually short connections. When a request ends, the connection to the server is disconnected. The server cannot actively push data to the client, but can only initiate a request from the client, real-time message transmission needs to be implemented on the web, such as instant chat and stock dynamic information.
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 tap into its potential, such as Gmail and Google Gtalk web pages, which are important applications of Ajax persistent connections. At present, many websites may already use the persistent connection technology, for example, instant chat on an SNS website.

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 webpage. 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 that 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 perform any operation, the connection is sleep, 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 in real time, with little latency. 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 may also cause too frequent refresh issues. 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.

I took a 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, then, the client re-initiates the next request. An important line in background judgmentCode: Usleep (10000) 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 the network transmission link in the middle. If it is an Ajax instant chat system, you need to save the user's chat records, then we need to combine it with the database to query data more than 10 times per second. This is a short connection ratio, which will definitely increase the pressure on the database. How can we optimize it, this 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.