Answer the question
In order to leave comments, you need to log in
What is the best way to do HTTP SSE (Server Side Events) in a PHP application?
In a classic PHP application running on Apache, it became necessary to send data to clients in real time when certain events occur. Specifically, external systems connect to the application via the API and add some data to it, the handler immediately processes them and must immediately notify all users on the site about the arrival of new data. Up to 300 simultaneously connected clients are expected. I would like to choose some simple and reliable implementation method, and preferably without installing an additional technology stack.
The most tempting option is to use HTTP SSE. Built-in and simple browser support, automatic connection recovery when disconnected. And allegedly the server side can be done on the same PHP. There are many examples on the Internet like this:
header("Content-Type: text/event-stream");
while (1) {
echo 'data: xxxxxxxxxxx';
ob_end_flush(); flush(); sleep(1);
}
Answer the question
In order to leave comments, you need to log in
What prevents when new data arrives, to mark somewhere in the database that "data has arrived"? And with your script you look to see if there is new data, if there is, send it to the client. In any case, you need to make different scripts, one for receiving data from services, the second for sending data to the client. And they must work independently. You can also make one script, but make several threads for tasks in it.
Clearly, the task is not as simple as it seems. One script with while(true) sleep(1) is not enough.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question