I am trying to implement a Server-Sent Event where the server will send the clients new messages.
The challenge here is that the server updated sessions and then echo their content.
The problem is that I will need to have that in one infinite loop.
Here is what I have done.
while(true){
session_start();
header("Content-Type: text/event-stream" . PHP_EOL);
header("Cache-Control: no-cache" . PHP_EOL);
//configure the connection
$conf = new ICWS\Config\Config($url, $stationName);
//create a new instance of icws
$attrebutes = array('AccoRDI_mid','AccoRDI_account_id');
$icws = new ICWS\Connection($conf, $attrebutes, true);
$messaging = new ICWS\Messaging($icws);
$messaging->processMessages();
session_write_close();
$result = $messaging->getCallsQueue();
echo 'event: getMessagingQueue' . PHP_EOL;
echo 'data: ' . json_encode( $result) . PHP_EOL;
ob_end_flush();
flush();
usleep($sleepTime * 1000000);
}
The problem here is that I get the following warning/notices
Warning: session_start(): Cannot send session cache limiter - headers already sent and
Notice: ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush i
The reason that I need the session_start()
in the loop is because I will need to unlock the session file immediately after reading it before the script sleeps.
The idea is to
- Start/resume session
- Update session values
- Display the content of the session
- Unlock/release the session to allow other processes to use it
- Go back to step 1
What can I do to avoid the errors here?