I'm using server-side event in PHP/jQuery. here is the client side:
var evtSource = new EventSource("testsse.php");
evtSource.onmessage = function(e) {
// DO SOMETHING WITH e
}
Here is the server side file : testsse.php
header("Content-Type: text/event-stream\n\n");
while (1) {
echo "message";
ob_flush();
flush();
sleep(1);
}
It works fine, but when the client close its browser or disconnect, the testsse.php continue running. My problem is that we quickly have many running scripts that overload the server. I already read the following questions:
I already read the following questions :
- How can i stop a server sent event script from running when the user closes the page? PHP (I can't modify the apach configuration)
- Does php execution stop after a user leaves the page?
- How do server-sent events actually work?
- How to stop Server-Sent Events
The last one is interesting, but that's finally the server that tell to the client this is the end. In our case, we would like the scripts automatically die when the client is closed. One solution is to fire an event when tab or browser is closed (Detect browser or tab closing). But unfortunately, this solution is known to be unreliable. And in my opinion, this is not a "professional" option.
Is there a way to properly kill the server side script when the client exit ?