T
T
tushev2019-12-05 13:41:51
PHP
tushev, 2019-12-05 13:41:51

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);
}

But it is not clear how in such a script it is better to wait for the event to occur in another PHP script? I definitely don't want to engage in cyclic polling. We need some kind of semaphores or something like that. And whether Apache will break long-hanging HTTP connections, after all, this is not quite its standard use. How to end the script when the client is disconnected?
I also looked at other options:
- Write a custom server in Node.JS. He seems to be just fine for this sort of thing. On the one hand, you listen to the API through which data is received, and scatter the received data across all hanging SSE connections. But I have no experience with Node.JS.
- Using WebSocket seemed to me redundant for this task. I only need to send notifications unilaterally.
- Laravel has a solution out of the box: Laravel + Redis + laravel-echo-server (on Node.JS) + Socket.IO + Laravel-Echo. But somehow I don’t want to drag this entire collective farm for the sake of one simple task. It uses WebSocket as a transport, but for some reason not SSE.
What is the best solution? Should I try to get by with one bare PHP + Apache and built-in browser support for SSE? Or it is fraught with pitfalls and it is better to drag an additional technology stack. What to choose?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
profesor08, 2019-12-05
@profesor08

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 question

Ask a Question

731 491 924 answers to any question