what is the correct way of catching model events and sending them to the browser as push notification. Basically what i want is to send notifications(SSE) to the browser when a new notification is created for a user. Currently i'm using Symfony StreamedResponse in the following method in a controller and it's not working.
'Notification' is an eloquent model having a 'user_id' field.
/* Notification Controller Stream method */
public function stream() {
if(Auth::check()) {
$user = Auth::user();
$response = new StreamedResponse(function() use ($user) {
Notification::created(function($notification) use ($user) {
if($notification->user_id == $user->id) {
echo "notification:" . json_encode(['notification' => $notification]) . "\n\n";
ob_flush();
flush();
}
});
$response->headers->set('Content-Type', 'text/event-stream');
return $response;
}
return 'Not logged in.';
}
If it helps : When i visit this method through its URL, the browser never starts loading. It ends up in an infinite loop. What am i doing wrong here? Any help is much appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire