So I am trying to use Server Sent Events in my laravel app but the problem is that in SSE, you can only specify single URL as in:
var evtSource = new EventSource("sse.php");
The problem is that I want to send events from different parts/controllers of the entire application not just single sse.php
file for example. Example:
// AuthController.php
public function postLogin(Request $request) {
// login logic
// SEND SSE EVENT ABOUT NEW USER LOGIN
}
// FooController.php
public function sendMessage() {
// SEND SSE EVENT ABOUT NEW MESSAGE
}
The only idea that comes to my mind is that from these controllers, create temporary files with event text and then read those text files in sse.php
file, send events and then delete files. However, this doesn't look like perfect solution. Or maybe hook sse.php
file with Laravel events mechanism somehow but that seems kinda advanced.
I also don't mind creating multiple event source objects with different php files like sse.php
but problem is that those controllers methods don't necessarily just send events alone, they do other work as well. For example, I cannot use postLogin
action directly in SSE object because it also performs the login logic not just sending event.
Has anyone had similar problem and how to tackle with it please ?