Server side events with php

2024-02-06 PHPJS

There are different ways for near real-time browser updates by a server message.
With a PHP website on the server side, you are very restricted, because in normal case you can't run a web socket process or something else.
In the past, the simplest way was to use long polling.
Long polling means:

if you use long polling, you have to implement the handling around your main logic
to handle and reconnect by connection interruption and so on.

With the EventSource Object, javascript now supports this more elegantly.

on the server side, it is like long polling.
you have to add some headers and can send data to the client without closing the connection.

header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");
set_time_limit(0);

while (true) {

    echo "event: date\n";
    echo "data: " . (new DateTime())->format('d.m.Y - H:i:s') . "\n\n";

    ob_flush();
    flush();

    sleep(2);
}

Here is a client-side JS example:

if (!window.EventSource) {
    alert("Server-Sent Events not supported");
    return;
}

const eventSource = new EventSource("server.php");

eventSource.addEventListener("date", function (event) {
    console.log(event.data);
});

eventSource.addEventListener("end", function (event) {
    eventSource.close();
});

eventSource.addEventListener("error", function (event) {
    if (event.readyState === EventSource.CLOSED) {
    console.log("Fehler aufgetreten - die Verbindung wurde getrennt.");
    }
});

links and more information

❰ back