We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

text/event-stream in Phalcon

I have an empty Controller with the Following Code:

public function streamAction() {
  $this->view->disable();
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache');
  header("Connection: keep-alive");

  for ($i = 1; $i <= 10; $i++) {
    echo "data: {".date('r')."}\n\n";
    ob_flush();
    flush();
    sleep(2);
  }
}

When I request this with my browser, it takes about 20 Seconds ( 2 Second wait per loop-iteration ) and after that I get all the Results at once. Which is not the expected behaviour of an event-stream.

When I use the same code in "native" PHP, I get a result "send" to my browser ever 2 Seconds. Which is how its supposed to be.

Any Ideas on how I could implemet this into my Phalcon Project?



77.7k
Accepted
answer
edited Jun '16

As Fabian wrote here: https://forum.phalcon.io/discussion/2449/output-buffer-

ob_start();
ob_start();
// do you stuff
ob_end_clean();
$output = ob_get_clean();

You'll have to keep that in mind when meddling with the ob ;]

edited Jun '16

sleep() is blocking your execution flow as exactly sleeping. Output buffer is flushed only once with your previously set headers.

@robertkaelin: I just tested your 'working' example, it does not work with both PHP 5.5.x nor PHP 7.0.x. It won't even load results in a browser with:

header('Content-Type: text/event-stream');

If I remove that header line, I wait loop x sleep seconds and only then get the output displayed.

And it just cannot work that way since the way how PHP is designed.

edited Jun '16

sleep() is blocking your execution flow as exactly sleeping. Output buffer is flushed only once with your previously set headers.

@robertkaelin: I just tested your 'working' example, it does not work with both PHP 5.5.x nor PHP 7.0.x. It won't even load results in a browser with:

header('Content-Type: text/event-stream');

If I remove that header line, I wait loop x sleep seconds and only then get the output displayed.

And it just cannot work that way since the way how PHP is designed.

@stamster: You are probably using a browser which does not support the text/event-streams I tested it in an online Sandbox for you, PHP is well designed enough to support it ;)

As Fabian wrote here: https://forum.phalcon.io/discussion/2449/output-buffer-

ob_start();
ob_start();
// do you stuff
ob_end_clean();
$output = ob_get_clean();

You'll have to keep that in mind when meddling with the ob ;]

Amazing :D

Well it's a bit messy, but it works great! Thank you very much!

Well, that's more workaround (or hack) then a solution. You flush output buffer each time while keeping connection open using Keep-Alive. Browser gets data through opened connection. Almost like web sockets.

I have tested with latest (v 47.0) Firefox browser. And those sandboxed environments are often interpreting code in a CLI mode.

At the end of the day, if it works the way you desire - use it :)

edited Jul '16

Well, that's more workaround (or hack) then a solution. You flush output buffer each time while keeping connection open using Keep-Alive. Browser gets data through opened connection. Almost like web sockets.

I have tested with latest (v 47.0) Firefox browser. And those sandboxed environments are often interpreting code in a CLI mode.

At the end of the day, if it works the way you desire - use it :)

@stamster Well what you describe in your answer is actaully the intended behaviour of an event-stream. ¯_(ツ)_/¯

And, yes i know how a sandbox works thank you, it was a demonstration. I tested it on my real environment, with chrome and firefox, the problem with it not working seems to be in your environment.

Just wanted to show you that, before you call someone out for their code not working, to at least take the time to test it properly.

Thank you for your input.

My environment? I have used official latest Mozilla Firefox and your 'working example code' on both PHP7 and PHP5.5, totally out of Phalcon realm, i.e. raw PHP. Is that what you're going to tell your clients, it's their environment problem and not your application / hack problem? :) Well good luck with that.

Just wanted to show you that befor you call someone out for their code not working to at least take the time to test it properly.

Well, I just wanted to point out that you should read posts in detail, as this is what I wrote in my previous answer:

@robertkaelin: I just tested your 'working' example, it does not work with both PHP 5.5.x nor PHP 7.0.x. It won't even load results in a browser

So, I tested and it does not work with your code example , sorry.

One more time - at the end of the day if it works for you - go ahead and use it and enjoy.

Cheers

My environment? I have used official latest Mozilla Firefox and your 'working example code' on both PHP7 and PHP5.5, totally out of Phalcon realm, i.e. raw PHP. Is that what you're going to tell your clients, it's their environment problem and not your application / hack problem? :) Well good luck with that.

Just wanted to show you that befor you call someone out for their code not working to at least take the time to test it properly.

Well, I just wanted to point out that you should read posts in detail, as this is what I wrote in my previous answer:

@robertkaelin: I just tested your 'working' example, it does not work with both PHP 5.5.x nor PHP 7.0.x. It won't even load results in a browser

So, I tested and it does not work with your code example , sorry.

One more time - at the end of the day if it works for you - go ahead and use it and enjoy.

Cheers

We don't seem to be making any progress here, and I have actual work to do, so for the sake of time I'm not going to continue responding to this.

Have a good one

edited Mar '19

I most want to say that you are doing a great job.https://eventeca.com/trade-show-app