I want to build a php script for server side events (SSE), that will notify connected clients about changes in the server-side data.
For example, I have a CRUD API. When an API endpoint is called, it does its own stuff and then somehow notifies the SSE script about the changes, which sends it back to all connected clients.
To do this I do not need websockets, one-way connection to clients is sufficient.
The problem is that most of SSE scripts examples use infinite loops like while(true){ sendData(); sleep(1); }
.
I can check for any changes in the data inside that loop, but that is very inefficient, because these changes are not so frequent.
What I really want to achieve is a real async interface between API script and SSE script. I've seen ReactPHP for event-driven programming, but how do I combine it with the SSE script. How to trigger SSE to send an event from external PHP script?