Server Sent Events


I’ve tried to set Server Sent Events. On production there is everything ok, but on LocalWP there is problem.

JS file create EventSource:

const evtSource = new EventSource(settings.ajax_url + '?action=sse' + '&nonce=' + settings.nonce);

and listen answears:

        evtSource.onopen = function () {
            console.log("Connection to server opened.");

        evtSource.onmessage = function (e) {

        evtSource.onerror = function (err) {
            console.error("EventSource failed:", err);

In action:

add_action('wp_ajax_sse', array($this, 'sse'));

I’ve loop where I try to echo answears:

    header("Content-Type: text/event-stream");
    header("Cache-Control: no-cache");

    while (true) {
        echo "event: message" . PHP_EOL;
        echo "id: $id" . PHP_EOL;
        echo "data: " . json_encode($msg) . PHP_EOL;
        echo PHP_EOL;

        while (ob_get_level() > 0) {

        if (connection_aborted()) break;


In Local WP flush() not working. On production server there is everything ok.

When I’m ending the script after e.g. 10 loops, I’ve receive all echos at once.

There is some settings with buffer, but any of changes not helped to send echos after each loop.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.