Abandon Ajax ?! Experience at the beginning of "server push"

Source: Internet
Author: User

Taking the chat room as an example, when talking about writing a chat room, everyone can say a rough story at will:

That is, an asynchronous request is sent to the server at intervals, and Ajax is replaced by four letters.

 

In fact, let's think about it. The purpose of AJAX is to update pages in real time. If our dynamic pages can be updated in real time, why are we still paying for this discount?

 

Let's take a look at a small experiment:

1 <?php2 while(true){3         echo 'Hello';4         sleep (1000);5 }6 ?>

If we want to, the page will display a 'hello' every second'

 

If you see this, you may know how to write the message display page in our chat room. Please refer to the pseudocode.

 1 <?php 2 $currentData = getData(); 3 while (true){ 4       $differences=getData(); 5       if ($differences!=$currentData){ 6                    echo $differences; 7                    } 8       sleep (10000); 9 }10 ?>

Unfortunately, this is not a success because we have not considered the PHP cache and buffer issues.

 

By default, PHP caches content to prevent header and data separation of HTTP packets. PHP will buffer the page content until the program ends and then output the page.

 

It's easy to know the reason. Fortunately, PHP provides methods to disable caching and caching.

Add

1 output_buffering=02 ob_end_flush();3 set_time_limit(0);

 

It is changed:

1 <? PHP 2 output_buffering = 0; 3 ob_end_flush (); 4 5 while (true) {6 echo 'hello'; 7 sleep (1000); 8 flush (); // force PHP to output 9} 10?>

You will find that the first hello user is successfully displayed!

Our message page becomes

 1 <?php 2 output_buffering=0; 3 ob_end_flush(); 4  5 $currentData = getData(); 6 while (true){ 7       $differences=getData(); 8       if ($differences!=$currentData){ 9                    echo $differences;10                    flush();11                    }12       sleep (10000);13 }14 ?>

 

 

However, in the final implementation of the project, the client also has problems:

Some browsers cache the received content before it is displayed. For example, the Netscape Browser caches content before it receives a line break or the beginning of an HTML Tag, and does not display the entire table until it receives the </table> tag.

Ie starts to display the page only after receiving the 256 bytes. Therefore, some extra spaces must be sent for these browsers to display the page content.

 

So far, we have used "server push" to solve the problem of real-time update. Other frameworks are no different from common ideas, so I will not go over them again.

 

If you are interested, you can write one by hand. Haha.

 

You are welcome to join us.

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.