Im trying to use SSE in PHP backend to send messages to a user.
the functional part of the backend code looks like-
<?php
set_time_limit(0);
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
ignore_user_abort(0);
@session_start();
ob_start();
echo "retry: 1000\n";
ob_flush();
flush();
require_once 'sql_connection_class.php';
require 'chatbox_class.php';
$chatbox=new chatboxclass($_GET['friendid']);
function recursivesenddata(){
global $chatbox;
$jsonobj=$chatbox->jsonloadunreadmessages();
//append json object
if($jsonobj!=-1){
> echo "data:{$jsonobj}\n\n";
> ob_flush();
> flush();
> }else{
> //don't send anything
> }
}
while(true){
recursivesenddata();
session_write_close();
sleep(1);
}
?>
it appears that ignore_user_abort(0) doesn't do anything when the user closes the page.
in $chatbox->jsonloadunreadmessages() there is a function that should only be executed when the page is open, it updates things in mySQL database. but this script keeps on running on the server even when the page is closed!
is there anyway to check on the server side when the user has closed the page to exit the infinite while loop?