I have a curious behavior with SSE.
I have 2 files, one for server side and one for browser client.
serverside.php is this:
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
ob_end_clean();
while( true ) {
set_time_limit(30);
$time = date('d/m/Y H:i:s');
echo 'data: ' . $time . "\n\n";
flush();
error_log('i am here!');
sleep(1);
}
?>
and client side file is this:
<!DOCTYPE html>
<html>
<body>
<div id="result"></div>
<script>
var source;
if(typeof(EventSource) === "undefined") {
alert('Problem with your browser.');
}
source = new EventSource("serverside.php");
source.onopen = function (event) {
console.log('streaming SSE opened');
};
source.onmessage = function(event) {
console.log('Streaming Message Received');
document.getElementById("result").innerHTML = event.data;
};
source.onerror = function (event) {
console.log('ERROR EventSource');
}
</script>
</body>
</html>
If i test this all is running ok. I got the refreshed time in browser and when i close the tab in my browser, serverside.php doesn't send more 'i am here!' message to log. That is the right behavior. When browser tab is closed, serverside.php must stop.
But now, if i remove or comment these lines in serverside.php file:
// echo 'data: ' . $time . "\n\n";
// flush();
the serverside.php file remains like this:
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
ob_end_clean();
while( true ) {
set_time_limit(30);
$time = date('d/m/Y H:i:s');
// echo 'data: ' . $time . "\n\n";
// flush();
error_log('i am here!');
sleep(1);
}
?>
That is, no flush data at all.
If now I load index.php i will not receive nothing from serverside.php. But in this case, when i close the browser tab, serverside.php continues working and sending to log 'i am here' messages.
Also, if serverside.php sends data and after some messages sent stops to send more data, the behavior is the same. 'i am here' messages continues to arrive after tab close.
- If I close browser tab while data is sending from serverside.php, all works ok.
- If I close browser tab after data was sent and nothing is sending from serverside.php, serverside.php continues working.
What is the explication for this?
I'm completely stuck.