I'm creating an infopage for work that updates "live".
Now i came across Server Sent Events.
I managed to put data on the screen but the update was every 3 seconds. (After looking online it appears i created a disconnect and the default retry time was 3 seconds)
So i found a sollution that in theory should work but when putting it live, it creates an overload on the server side.
<?php
// make session read-only
session_start();
session_write_close();
// disable default disconnect checks
ignore_user_abort(true);
// set headers for stream
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");
// Is this a new stream or an existing one?
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
if ($lastEventId == 0) {
$lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
}
echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
echo "retry: 1000\n";
// start stream
while(true){
if(connection_aborted()){
exit();
} else{
// here you will want to get the latest event id you have created on the server, but for now we will increment and force an update
$latestEventId = $lastEventId+1;
if($lastEventId < $latestEventId){
echo "id: " . $latestEventId . "\n";
echo "data: Howdy (".$latestEventId.") \n\n";
$lastEventId = $latestEventId;
ob_flush();
flush();
} else{
// no new data to send
echo ": heartbeat\n\n";
ob_flush();
flush();
}
}
// 2 second sleep then carry on
sleep(1);
}
?>
It appears that the while(true) loop is the problem ...
Unfortunatly i couldn't find the correct way to use Server Side events with a connection that stays open.
Is there someone that knows a sollution for this?
i tried using this script without while loop (which works) BUT thats because the client side keeps reconnecting after every 3 seconds, so basically the connection opens, closes, opens, closes
I'm creating an infopage for work that updates "live".
Now i came across Server Sent Events.
I managed to put data on the screen but the update was every 3 seconds. (After looking online it appears i created a disconnect and the default retry time was 3 seconds)
So i found a sollution that in theory should work but when putting it live, it creates an overload on the server side.
<?php
// make session read-only
session_start();
session_write_close();
// disable default disconnect checks
ignore_user_abort(true);
// set headers for stream
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");
// Is this a new stream or an existing one?
$lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
if ($lastEventId == 0) {
$lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
}
echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE
echo "retry: 1000\n";
// start stream
while(true){
if(connection_aborted()){
exit();
} else{
// here you will want to get the latest event id you have created on the server, but for now we will increment and force an update
$latestEventId = $lastEventId+1;
if($lastEventId < $latestEventId){
echo "id: " . $latestEventId . "\n";
echo "data: Howdy (".$latestEventId.") \n\n";
$lastEventId = $latestEventId;
ob_flush();
flush();
} else{
// no new data to send
echo ": heartbeat\n\n";
ob_flush();
flush();
}
}
// 2 second sleep then carry on
sleep(1);
}
?>
It appears that the while(true) loop is the problem ...
Unfortunatly i couldn't find the correct way to use Server Side events with a connection that stays open.
Is there someone that knows a sollution for this?
i tried using this script without while loop (which works) BUT thats because the client side keeps reconnecting after every 3 seconds, so basically the connection opens, closes, opens, closes
Where is this hosted? If your own, the server logs might give a clue. If shared hosting, long-running processes are usually killed after a certain amount of time.
The other main troubleshooting idea is to watch what is happening in the Network tab in developer tools in the browser. It will show what data is sent, and if it is the server-side or client-side that aborts the connection.
Also try connecting via curl, to confirm the problems happen there.
I think there is a bug in your code as shown:
echo ": heartbeat\n\n";
It should read:
echo "data: heartbeat\n\n";
I don't know if that is triggering the problems you see, but I'd fix it. (I'd also recommend putting the datestamp in, rather than a fixed string. It will help troubleshooting.)