Around 2017 I wrote a SSE (PHP) script and the JavaScript code. It worked fine with PHP-5 + HTTP/1 and the Firefox of that era.
The same code now does not work with Firefox-140 esr or latest Chrome with PHP-8.2 + HTTP/2. I have a time-out after 60 seconds.
Also tried the simplest SSE example from lucidar.me, zip. If I remove the while loop something that I think it's bad, it's working. In that case the JavaScript makes requests every 5 seconds. I think this defeats the purpose of SSE.
The code with while works fine with curl but not with browsers.
My implementation and the rationale of SSE is that the server decides when to send the data, not the client. Right?
It changed something in JavaScript I guess, in browsers, or did I miss something? It's the HTTP/2 the problem?
The code works with curl like that (while version)
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache, no-transform');
header('X-Accel-Buffering: no');
header('Access-Control-Allow-Origin: *');
set_time_limit(0);
@ini_set('zlib.output_compression', 0);
@ob_implicit_flush(true);
// Loop until the client close the stream
while (true) {
// Echo time
$time = date('r');
echo "data: The server time is: {$time}\n\n";
// Flush buffer (force sending data to client)
@ob_flush();
flush();
// Wait 2 seconds for the next message / event
sleep(2);
}
?>
Added info after @Hybrid gave me an idea.
Forgot to mention that I use apache. I switched to HTTP/1 and the result is bizarre. curl now responds after 2 minutes. With HTTP/2 the response was instant. Now Firefox responds like curl.
Testing with HTTP/1 only server
export LC_ALL=C; date; curl https://XXX/sse.php
Fri Jul 25 23:17:48 EEST 2025 <-- date invoked
data: The server time is: Sat, 26 Jul 2025 00:19:18 +0300 <-- received after 1 minute and 30 seconds.
data: The server time is: Sat, 26 Jul 2025 00:19:20 +0300
data: The server time is: Sat, 26 Jul 2025 00:19:22 +0300
... total 58 lines of data: received.
Here testing with HTTP/2 again. Only curl works, not browsers.
curl -v URL by-default uses HTTP/2:
* ALPN: curl offers h2,http/1.1
* ALPN: server accepted h2
So browsers, can't handle SSE with HTTP/2?
addps(add packed single-precision floats). server-sent-events is the one you want. When you find two tags which you think might apply, read their mouseovers. Really not cool to remove the correct tag and add the wrong tag, especially when there's already a comment about those tags. I already fixed the tags on this question once today.