S
S
Sergey Korenevsky2021-07-07 14:22:05
PHP
Sergey Korenevsky, 2021-07-07 14:22:05

Why do server SSE messages only come when a connection error occurs?

Why do server messages come all immediately after the initialization of the last error of receiving a message from the server?

date_default_timezone_set('Europe/Moscow'); 
header('Cache-Control: no-cache\n\n');
header("Content-Type: text/event-stream\n\n");
//header("Connection: keep-alive\n\n");
$length = 20;
while(++$length > 0){
  echo "data: 2 $DevID  Start Stream !\n\n"; 
  ob_end_flush();
  flush();
  sleep(5);
}

On the client, of course.
const evtSource = new EventSource('event.php');
evtSource.onopen = function(open){
  console.log('Open EventSource:  \n',open);
};
evtSource.onmessage = function(event){
  console.log('Work EventSource!!!! \n',event.data);        
};
evtSource.onerror = function(err){
  console.log('Error EventSource:  \n',err);
};

Those. the browser console first displays a message about opening a connection. And then silence for a very long time. And then all messages immediately fall out with a final error message.
According to the description https://developer.mozilla.org/ru/docs/Web/API/Serv...
I did everything right.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-07-07
@Dier_Sergio_Great

I have never used invets, I like websockets more, and even there I used a ready-made library
on the topic - the question is, what kind of web server is it, how does php (cgi or a module) start, because maybe the server is to blame here and not the
ps code if your code is run in php -S then everything works (swears at $DevID by itself and the startup time limit is 30 seconds but continues to work)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question