Slim: [Feature Discussion] Adding SSE requests to Slim

Created on 14 Aug 2016  Â·  9Comments  Â·  Source: slimphp/Slim

I wanted to start a discussion about if SSE (Server Sent Events) would be a nice addition to Slim, if it would be useful enough to even think about adding or even possible to add.

I recently had a project which needed server bound event triggers, I wasn't able to use Node which for this kind of project I would tend to use and I didn't want to use polling, sort or long, so I chose to use SSE to handle the page updates.

With this project I used Slim, however the SSE endpoints were not able to be used within a Slim response.

What are everyones thoughts on this, would it be nice to allow SSE requests / responses, these requests aren't like standard open and close requests so not sure how they fall in the PSR-7 standard.

Slim 4 discussion new feature

Most helpful comment

SSE's basically create a socket like request over HTTP, however the connection doesn't close unless you ask it to be closed, if the browser hears it close it automatically reopens the connection to begin listening again.

They are an HTML5 alternative to web sockets, long polling and short polling, some may say they are still polling but with the added extra of having the browser handle them instead of the developer.

if you open the browsers dev tools and reload the example, you will see the request to stocks.php and the request doesn't have a http response but instead it has a eventstream auto updating with the data it receives from the server.

Example: (Uses SSE to update stocks live)
http://demo.howopensource.com/sse/

Information / Tutorials:
http://www.howopensource.com/2014/12/introduction-to-server-sent-events/
http://www.htmlgoodies.com/beyond/reference/receive-updates-from-the-server-using-the-eventsource.html
http://www.w3schools.com/html/html5_serversentevents.asp

EDIT: Additional link with spec information and a note
https://html.spec.whatwg.org/multipage/comms.html#server-sent-events

NOTE: IE doesn't support EventSource so the Slim endpoint for SSE will need to also allow for a fallback, http://stackoverflow.com/questions/24498141/is-there-a-microsoft-equivalent-for-html5-server-sent-events

All 9 comments

Could you provide links to any explanatory texts for what they are, in your understanding? It sounds interesting.

SSE's basically create a socket like request over HTTP, however the connection doesn't close unless you ask it to be closed, if the browser hears it close it automatically reopens the connection to begin listening again.

They are an HTML5 alternative to web sockets, long polling and short polling, some may say they are still polling but with the added extra of having the browser handle them instead of the developer.

if you open the browsers dev tools and reload the example, you will see the request to stocks.php and the request doesn't have a http response but instead it has a eventstream auto updating with the data it receives from the server.

Example: (Uses SSE to update stocks live)
http://demo.howopensource.com/sse/

Information / Tutorials:
http://www.howopensource.com/2014/12/introduction-to-server-sent-events/
http://www.htmlgoodies.com/beyond/reference/receive-updates-from-the-server-using-the-eventsource.html
http://www.w3schools.com/html/html5_serversentevents.asp

EDIT: Additional link with spec information and a note
https://html.spec.whatwg.org/multipage/comms.html#server-sent-events

NOTE: IE doesn't support EventSource so the Slim endpoint for SSE will need to also allow for a fallback, http://stackoverflow.com/questions/24498141/is-there-a-microsoft-equivalent-for-html5-server-sent-events

Thanks for the context, it will help the discussion a lot!

Since they are just plain old HTTP it is possible already to serve SSEs with Slim. Let's assume you have the following JavaScript.

var source = new EventSource("/events");
source.onmessage = function(event) {
    console.log(event.data);
};

Simple SSE route would look like this.

$app->get("/events", function ($request, $response, $arguments) {
    $data = time();
    return $response
        ->withHeader("Content-Type", "text/event-stream")
        ->withHeader("Cache-Control", "no-cache")
        ->write("data: {$data}\n\n");
});

However this closes the connection and it makes EventSource automatically reconnect every three seconds which effectively makes it polling.

It is possible to make the /events route to stream the data by not using PSR-7 and going into a loop. However this is a bit kludgish and not the best thing to do with Apache. It will also eat up you maximum concurrent connections.

$app->get("/events-loop", function ($request, $response, $arguments) {
    header("Content-Type: text/event-stream");
    header("Cache-Control: no-cache");
    ob_end_clean();
    ob_implicit_flush();
    while (true) {
        $data = time();
        print "data: {$data}\n\n";
        usleep(1000000);
    }
});

You also need to turn off PHP buffering in .htaccess file for this to work.

php_value output_buffering Off

Just wanted to see if this should be considered for the v4.0 milestone or not, I would like to see this be a built-in feature of version 4 so developers can use SSE's without much work to get it working, something Slim is really good at, writing little but being able to do a lot.

What are everyones thoughts on including this in the v4.0 milestone?

Edit: one solution ca be found here ... http://discourse.slimframework.com/t/how-to-use-html5-server-sent-events-sse/879/5

You have my vote

I suspect that this would be solved id #2481 is implemented?

Yep. Thx.

On Sun, 16 Sep 2018, 14:21 Rob Allen, notifications@github.com wrote:

I suspect that this would be solved id #2481
https://github.com/slimphp/Slim/issues/2481 is implemented?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/slimphp/Slim/issues/1959#issuecomment-421756316, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AKyv8-Rg4PNlSH3bYjyp1FaRq4pDs-Usks5ubkJlgaJpZM4Jj5Tn
.

Closing in favour of #2481.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arokettu picture arokettu  Â·  3Comments

basuke picture basuke  Â·  3Comments

codeguy picture codeguy  Â·  4Comments

lwiwala picture lwiwala  Â·  5Comments

codeguy picture codeguy  Â·  3Comments