Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
<?php
    ob_start();
    echo 'test';
    ob_end_flush();
    flush();
    sleep(10);

I tried to output response for ajax request before the connection finishes,but not working--it will wait 10 seconds before successfully fetching response.Is it the problem of http server,in my case Apache?If that's true,how to fix it so that it echos back the response right away?

share|improve this question
    
Is PHP configured as an Apache Module or as a CGI extension? –  Andrew Moore Jan 20 '10 at 16:31
    
@Andrew Moore ,apache module. –  vps Jan 20 '10 at 16:34

2 Answers 2

While flush(), ob_end_flush() and ob_flush() is supposed to guarantee that output is sent to the server, I've found that PHP is sometimes not very forgiving of particular software configuration.

On Windows especially, output buffering can be a pain to setup and the only way I've found that works reliably between systems is to reset output buffering on every flush like so:

<?php
obf_start();

for($i=0;$i<70;$i++) {
    echo 'printing... ', microtime(true), '<br />';

    if($i % 3 == 2) obf_flush();

    usleep(300000);
}

function obf_start() {
    ob_start();
}

function obf_flush() {
    ob_end_flush();
    ob_flush();
    flush();
    obf_start();
}

Is it an ugly solution? Yes it is. But until all the bugs related to output buffering are fixed, I don't see this work-around going away in production code any time soon.

Also note that some anti-virus software (Panda AV and others) and proxies will hold all data until the socket is closed. You cannot guaranty that all clients will receive proper flushed data.

share|improve this answer
    
Wow,this solution is cool though ugly.But why doesn't it work with ajax requests?I replaced msgsrv.php with your code,it's here:stackoverflow.com/questions/333664/… –  vps Jan 20 '10 at 17:14
    
Because success is only called once the request is terminated with a 200 OK status. (ie.: socket is closed). –  Andrew Moore Jan 20 '10 at 17:23

The simplest way to know if something is a server or browser/library issue is to monitor the request live. The 2 simplest ways of doing this in your case are:

  • Firebug: This firefox extension will show you all your AJAX requests live as they are sent, and will show you the response as soon as it is received by the browser.
  • Traffic Capture: Use a tool such as Wireshark to see any type of request being sent and the tcp traffic coming back from the server in real-time.

If you see the data coming back right away but taking 10sec to display, your browser or AJAX library is waiting for the connection to close before processing the response. If it takes 10sec before you are getting anything back, your server or PHP module is at fault here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.