Hello I have a javascript code that listens to a PHP code via Server-Sent Events, it is working well the response is sent from the server trough a loop and when the loop end
You could use a boolean variable isOpenOnce = false in your javascript and then in your sse.onopen set isOpen to true the first time. In the beginning check the flag and close connection if true. This way no need of server side changes.
var isOpenOnce = false;
sse.onopen = function() {
if(isOpenOnce) {
sse.close();
}else {
console.log("Connection to server opened.");
isOpenOnce = true;
}
}