1

Is possible to send data to browser multiple times per request? For example my CMS will install some module and i want to send info to browser (downloading module, unpacking module, installing module, ...) and update some div or something for better UI feel. Maybe use flash??

Krab
  • 6,526
  • 6
  • 41
  • 78
  • I think you can do this using PHP's [output buffering](http://php.net/manual/en/book.outcontrol.php), so give that a look. – Bojangles May 26 '12 at 19:15
  • The output buffering thing is not that easy to control, apache settings AFAIK can override your attempts to fragment data sent to the browser and buffer for you even if you decided to flush. – Mihai Stancu May 26 '12 at 19:22
  • Mihay: Yes :/. Is some other way how realize this thing? I think i see this behavior on some web. – Krab May 26 '12 at 19:28

3 Answers3

3

I've done something like this in my web application. getstatus.php is a file which returns things like downloading module..., unpacking module..., which is updated by the php script that runs all those things, run.php here.

I use the following javascript code (using jQuery) on a different page, load.php, to show the progress:

<script type="text/javascript">
    var flag = false;

    $(function() {
        startProcess();
        getStatus();
    });  

    function startProcess() {
        $.get('run.php', function() {
            flag = true;
            window.location = 'example.com/finish.html'; // redirect when finished
        });
    }

    function getStatus() {
        $.ajax({
            url: 'getstatus.php',
            success: function(data) {
                // do something with the output of getstatus.php here
            }
        });
        setTimeout("getStatus()",500);
    }
</script>
Jeroen
  • 13,056
  • 4
  • 42
  • 63
  • Niceee, looks great, thx. Do you do some read/write synchronization and what do you use like storage for status, session? – Krab May 26 '12 at 20:04
  • No, I don't do some read/write synchronization, and I use simple text files as storage – Jeroen May 26 '12 at 20:17
0

If your actions (downloading, installing, etc.) are blocking the script's execution, you can simply put a message between each of them. Otherwise, you can give Web sockets a try.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
0

Use Ajax CORS :

How can I communicate over TCP sockets from JavaScript?

http://remysharp.com/2011/04/21/getting-cors-working/

it's a trick to simulate RealTime communication.

Good Luck

Community
  • 1
  • 1
xeonarno
  • 426
  • 6
  • 17