menu

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?

 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

  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);
  5.          
  6. //Flush (send) the output buffer and turn off output buffering
  7. while (@ob_end_flush());
  8.          
  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();

References

81 comments:

  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.

    ReplyDelete
  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?

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

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

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

    ReplyDelete
    Replies
    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 :)

      Delete
  5. It works perfectly in FF.

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

    Thanks

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete

  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

    ReplyDelete

  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

    ReplyDelete
  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

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    Good discussion.
    Six Sigma Training in Abu Dhabi
    Six Sigma Training in Dammam
    Six Sigma Training in Riyadh

    ReplyDelete
  16. Very useful and informative content has been shared out here, Thanks for sharing it.
    Visit Learn Digital Academy for more information on Digital marketing course in Bangalore https://www.learndigital.co/.

    ReplyDelete
  17. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information......
    oneplus mobile service center in chennai
    oneplus mobile service center
    oneplus mobile service centre in chennai
    oneplus mobile service centre
    oneplus service center near me
    oneplus service
    oneplus service centres in chennai
    oneplus service center velachery
    oneplus service center in vadapalani

    ReplyDelete
  18. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
    Thanks & Regards,
    VRIT Professionals,
    No.1 Leading Web Designing Training Institute In Chennai.

    And also those who are looking for
    Web Designing Training Institute in Chennai
    SEO Training Institute in Chennai
    Photoshop Training Institute in Chennai
    PHP & Mysql Training Institute in Chennai
    Android Training Institute in Chennai

    ReplyDelete
  19. Thanks for provide great information and looking beautiful blog. If you wish to learn get connected with Advanced Cyber Security Course. I hope below information will help you.
    Ethical Hacking Training in chennai
    Best Training Institute in Chennai

    ReplyDelete
  20. Nice Post. If you wish to get study abroad in any course. The below information will suit you.
    Skolarrssolutions
    Mbbs in Russia
    Mbbs in UK

    ReplyDelete
  21. GBWhatsApp is a far more convenient and modified version of WhatsApp – available for use by everyone on the Internet. Currently, the application only operates on Android smartphones and is not supported by iOS devices.for more Updates Visit Our click here

    ReplyDelete
  22. Whatsapp Group Link - 5000+ Updated Whatsapp Groups
    whatsapp group link Click The link And Join Whatsapp New Groups, Dating, Business promositions Groups

    ReplyDelete
  23. Thanks For sharing a nice post about vmware Training Course.It is very helpful and vmware useful for us.vmware training in bangalore

    ReplyDelete
  24. I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge.SAS Training in Bangalore

    ReplyDelete
  25. I Love your article. You can visit my website : m.bee.mp3.com

    ReplyDelete
  26. Thank you so much for such an wonderful blog. I’m really thankful to you for this informative Blog .

    Computer Repairing Course in Delhi

    ReplyDelete
  27. Amazing post i will bookmark this page for my future reference thanks ...
    clinical sas training in chennai
    Automation training chennai

    ReplyDelete
  28. looking for an amazing game for your android or ios device don't worry check this out download GTA 5 for android full apk free no survey

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. Snapdeal Winner List here came up with an Offer where you can win Snapdeal prize list 2020 by just playing a game & win prizes.
    Snapdeal winner name2020 also check the Snapdeal lucky draw2020

    ReplyDelete
  31. You can download free games and apps on your android mobile tweakbox apk

    ReplyDelete
  32. Hello Admin, First off Congratulations on this Post and Nice information for a new blogger…it is really helpful.
    Nepmod

    ReplyDelete
  33. It’s difficult to find experienced people in this particular topic, however, you seem like you know what you’re talking about! Thanks
    Tech news

    ReplyDelete
  34. Thanks for sharing the great usefull blog
    Shopclues lucky draw 2020| Shopclues winner 2020|Get to Know Shopclues Lucky Draw Winner 2020. Call Shopclues Helpline Phone Number+91-9835565523 for Shopclues Online Shopping Lucky Draw.
    Shopclues online lucky draw winner
    Shopclues winner 2020
    shopclues car lucky draw

    ReplyDelete

  35. Astonishing article that I have never seen before. I aimed to write an article based on this tally field, but my startings were rejected...Now, the information that you have designed in this blog was misery to read about this tally and its technical features. Surely these writings will help the beginners and the professionals. Thanks

    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai
    seo training classes
    seo training course
    seo training institute in chennai
    seo training institutes
    seo courses in chennai
    seo institutes in chennai
    seo classes in chennai
    seo training center in chennai

    ReplyDelete
  36. if you are searching for the result of Matka, Satta Matka, Boss Matka, Kalyan Matka Tips, Satta Matka, Matka, Sattamatka, Matka Satta, Matka Charts, Matka Satta Result, Fix Matka

    ReplyDelete
  37. I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge.
    AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  38. We provide best Selenium training in Bangalore, automation testing with live projects. Cucumber, Java Selenium and Software Testing Training in Bangalore.

    ReplyDelete
  39. Thank you for sharing great information with us. Your contribution is very unique and all information is reliable for new readers. Keep up the good work in the future, thank you for sharing this useful post. mobile price in bangladesh

    ReplyDelete
  40. Hi This is SRK i'm suggesting you this is app VidMate Apk

    ReplyDelete