I’ve implemented SSE in my Laravel application. It is working fine. But, it blocks the web application for the amount of time SSE is being executed.
public function index(Request $request)
{
$response = new SymfonyComponentHttpFoundationStreamedResponse(function() use ($request) {
while(true) {
$data = $this->getData($request->currency);
if ($data->hasChanged) {
echo 'data: ' . json_encode($data->rates) . "nn";
ob_flush();
flush();
}
sleep(0.5);
}
});
$response->headers->set('Content-Type', 'text/event-stream');
$response->headers->set('X-Accel-Buffering', 'no');
$response->headers->set('Cach-Control', 'no-cache');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
}
I have also tried session_write_close();
but it didn’t work.
Is there any solution to fix it in Laravel?