Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Fuel controllers and Server Events
  • Hi,

    I am setting up a site with server events following a simple schema with a while loop like this:
    https://github.com/html5rocks/www.html5rocks.com/blob/master/content/tutorials/eventsource/basics/static/demo/sse.php

    but I can't make it work in fuel.
    I have been fighting with the Response object, but I can't get fuel to send more than 1 response with a single controller call.
    If I put the response->send() in the while loop, it does not work.
    It only works with return $response; or even with die().
    Then it sends all accumulated events at once.

    I have tried REST controller and base/template controller.

    What would be the proper way to do this within fuel?

    Now what I have is the client polling the server (running the controller once and dying/returning), but this is not what server events should do.

    Thank you very much!
  • HarroHarro
    Accepted Answer
    You can't do that in Fuel, it's a framework made for request/response operations, it's not a continues listener which you need for this kind of implementation.

    It requires a streamed response to be able to send continues updates, which Fuel v1 does not support.
  • HarroHarro
    Accepted Answer
    What you can try in your loop is an explicit

    ob_flush();
    flush();

    after echo'ing out your update.

    Please note that this means a PHP process running for every client, which is not very efficient, and may cause your webserver to overload if you have more than a few clients. Also, create a solution to terminate the while loop, otherwise the processes run forever, even when the client is gone. And then there's PHP's execution timeout, which will abort the script unless you have disabled it.

    You might also want to have a look at Symfony's StreamedResponse which is part of it's HTTP foundation package, and which is designed to handle this kind of scenario's.
  • Hi Harro,

    as always thank you very much for your time and answer.
    That is what I had thought (after lots of testing).

    I have to say that the
    ob_flush();
    flush();
     approach does not work, either.

    I will try your suggested Symfony's StreamedResponse, looks very good.

    Thanks a lot, kind regards

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion