Friday, November 8, 2013

Real-time updating of php output using flush() in all browsers

Heyy guys,

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 isnt convinient 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 isnt any fault of php, of course if you run php from the terminal (WAMP users dont look confused now) you'll see the output in realtime. So what's preventing it?


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

  1. echo "add this";
  2. sleep(5);
  3. echo "Program Output";

output [the whole thing displays after 5 seconds]

  1. add thisProgram output

php with buffering off

  1. echo "add this";
  2. echo str_pad("",1024," "); //BROWSER TWEAKS
  3. echo " <br />" //BROWSER TWEAKS
  4. //telling php to show the stuff as soon as echo is done
  5. ob_flush();
  6. flush();
  7. //just a usual sleep
  8. sleep(5);        
  9. //second echo statement
  10. echo "Program Output";
  11. ob_flush();
  12. flush();

output [the whole thing displays after 5 seconds]

  1. 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-ass buffering and caching going. But you wont need all of these, just do trial and error to find the best methods for you.

  1. // Turn off output buffering
  2. ini_set('output_buffering', 'off');
  3. // Turn off PHP output compression
  4. ini_set('zlib.output_compression', false);
  6. //Flush (send) the output buffer and turn off output buffering
  7. while (@ob_end_flush());
  9. // Implicitly flush the buffer(s)
  10. ini_set('implicit_flush', true);
  11. ob_implicit_flush(true);
  12. echo "add this";
  13. echo str_pad("",1024," ");
  14. echo "<br />";
  15. ob_flush();
  16. flush();
  17. sleep(5);        
  18. echo "Program Output";
  19. ob_flush();
  20. flush();



  1. Its really helpful for me to understand. Thanks.
    If anyone wants to Learn Web Designing in Chennai go to the Besant Technologies which is No.1 Training Institute in Chennai.

  2. Thanks. I'm trying to display the output from a unix command line program. how would this work with a system() call that outputs every few seconds?

    1. does the php script call the system() command?

  3. Much appreciated. I'm attempting to show the yield from an unix charge line program.
    Best Web Design

  4. Doesn't work for me in FF or Chrome, strange

    1. tested on FF/chrome/IE at the time of writing the script, try increasing the str_pad value from 1024 -> 2048 and on, might resolve :)

  5. It works perfectly in FF.

    I deleted " ob_flush();" because I received warnings about it.


  6. After days of trial and error I finally came across this page. Worked perfectly for me. Thank you.

  7. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    QTP Training in Chennai

  8. In database computing, Oracle Real Application Clusters (RAC) — an option for the Oracle Database software produced by Oracle Corporation and introduced in 2001 with Oracle9i — provides software for clustering and high availability in Oracle database environments. Oracle Corporation includes RAC with the Standard Edition, provided the nodes are clustered using Oracle Clusterware.
    Oracle RAC allows multiple computers to run Oracle RDBMS software simultaneously while accessing a single database, thus providing clustering.

    In a non-RAC Oracle database, a single instance accesses a single database. The database consists of a collection of data files, control files, and redo logs located on disk. The instance comprises the collection of Oracle-related memory and operating system processes that run on a computer system.

    Oracle RAC Training in Chennai

  9. great article!!!!!This is very importent information for us.I like all content and information.I have read it.You know more about this please visit again.
    mainframe developer Training in Chennai

  10. This comment has been removed by the author.


  11. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

    Peridot Systems Chennai


  12. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.

    SEO training in Adyar

  13. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Android Training in Chennai
    Ios Training in Chennai

  14. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    QlikView Training in Chennai
    Informatica Training in Chennai
    Python Training in Chennai