Real-time PHP Output Update with flush() In All Browsers

Discussion in 'PHP' started by ManzZup, Nov 8, 2013.

  1. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    These days, i'm kinda into hot stuff each day but kinda not having time to document all those. So here I got a little time, let's skip the bla bla.

    What exactly is real-time updating means?



    Have you ever seen how php output is shown on the browser? No matter what length code or how many loops you put into, the browser always wait to get EVERYTHING, every tiny thing from php output before showing it to the user. Now this isn't convenient eh?

    Let's take a scenario where you have to get 1000 records from a database and do very high time consuming calculations on each record. So under normal conditions the user will be looking at a blank page for several minutes [or hours depending on your processor :D ]. This isn't any fault of php, of course if you run php from the terminal (WAMP users don't look confused now) you'll see the output in real-time. So what's preventing it?

    BUFFERING

    The Quickie Solve



    Just like every other problem there's the easy short code to do this. Tested with Chrome and Firefox in a standard LAMP server without any server config changes

    Usual php output code
    PHP:
    echo "add this";
    sleep(5);
    echo 
    "Program Output";
    output [the whole thing displays after 5 seconds]
    Code:
    add thisProgram output
    
    php with buffering off
    PHP:
    echo "add this";
     
    echo 
    str_pad("",1024," "); //BROWSER TWEAKS
    echo " <br />" //BROWSER TWEAKS
     
    //telling php to show the stuff as soon as echo is done
    ob_flush();
    flush();
     
    //just a usual sleep
    sleep(5);        
     
    //second echo statement
    echo "Program Output";
     
    ob_flush();
    flush();
    output [the whole thing displays after 5 seconds]
    Code:
    add this
    [waiting 5 seconds]
    Program output
    
    So let's talk about buffering. The feature-thingy that put the whole output at last is because there are several layers of buffering undergoing on the php output to make the output process efficient
    1. By php : the ob_* functions are related to this, ob_flush() would stop/flush the buffer
    2. By server : gzip compression, output_buffering in php5.ini does these
    3. By client : browsers like to do less work, so they wait for considerable about of data to come before showing them to the user

    Important tweaks of the code



    1) echo str_pad("",1024," "); //BROWSER TWEAKS

    As i mentioned browsers do their own buffering to wait for some amount of data before showing content to user
    FF - 512 bytes
    chrome - 1024 bytes
    IE - why would you ask? :D
    What above code does is adding 1024 blanks to the output, just to make it complete

    2) echo " <br />" //BROWSER TWEAKS

    Even after above tweak i found that Chrome doesn't show the output as expected. After few more hours of trial and error [at the workplace] I found that it need to read a newline before actually giving any output. Strange huh? So i simply added a html newline and VIOLA :D

    Now the code seems working but only buffering (1) and (3) seem to be addressed. So lets make the code complete in a case the server had some bad-*** buffering and caching going. But you wont need all of these, just do trial and error to find the best methods for you.

    PHP:
    // Turn off output buffering
    ini_set('output_buffering''off');
    // Turn off PHP output compression
    ini_set('zlib.output_compression'false);
             
    //Flush (send) the output buffer and turn off output buffering
    while (@ob_end_flush());
             
    // Implicitly flush the buffer(s)
    ini_set('implicit_flush'true);
    ob_implicit_flush(true);
     
    echo 
    "add this";
     
    echo 
    str_pad("",1024," ");
    echo 
    "<br />";
     
    ob_flush();
    flush();
     
    sleep(5);        
     
    echo 
    "Program Output";
    ob_flush();
    flush();

    References


    1. ob_flush()
    2. flush()
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice