timeLimit; $lastHeartbeat = time(); while (!connection_aborted() && time() < $expirationTime) { set_time_limit($this->execLimit); try { $data = call_user_func($callback); if ($data !== NULL) { $this->send($data); $lastHeartbeat = time(); } } catch (StopEventLoopException $th) { break; } // sleep and perform heartbeat to ensure connection is still alive for ($i = 0; $i < $this->interval; $i++) { if (time() >= $lastHeartbeat + $this->heartbeat) { echo ": \n\n"; ob_end_flush(); flush(); $lastHeartbeat = time(); } sleep(1); } } } /** * Send data to client encoded as json */ private function send($data): void { echo "data: " . json_encode($data); echo "\n\n"; // send data to stream ob_end_flush(); flush(); } }