You can use Laravel events and create a listener that combines all the different events from your application into a single thread, and then you would have one controller that would dispatch this stream of events to the client. You can generate an event anywhere in your code, and it will be transmitted to the client. You will need some kind of common FIFO buffer to provide communication between the listener and the controller, the listener will write to it, and the controller will read it and send SSE. The easiest solution is to use a simple log file for this.
Laravel also has a built-in broadcast feature using Laravel Echo, so maybe this can help? I'm not sure if Echo uses SSE (has zero experience with both), but it does almost the same thing ...
Updated: Let me try adding an example:
1) First we need to create an event and a listener, for example:
php artisan make:event SendSSE php artisan make:listener SendSSEListener --event=SendSSE
2) An event class is just a wrapper around the data that you want to pass to the listener. Add as many properties to the SendSSE class as you need, but only pass a string message for this example:
public function __construct($msg) { $this->message = $msg; } public function getMessage() { return $this->message; }
3) Now you can fire this event as follows:
event(new \App\Events\SendSSE('Hello there'));
4) Your recipient's handle () method will receive it, and then you need to push it into the FIFO buffer, which you will use to communicate with the SSE controller. It can be as simple as writing to a file and then reading from it in your controller, or you can use the DB channel or linux fifo or shared memory or whatever. I will leave this part to you and suppose that you have a service for it:
public function handle(\App\Events\SendSSE $event) {
5) Finally, in your SSE controller, you must read the fifo buffer and when a new message is sent to the client:
while(1) { // if this read doesn't block, than you'll probably need to sleep() if ($new_msg = FifoBufferService::read()) { $this->sendSSE($new_msg); } }