Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Server sent events don't function properly on live site

My SSE script check every second if there is new data and if so push it to my frontend.

In the code i have pasted here i have simplified the idea. On my localhost it will count to 10 in 10 seconds, then stop the php script and the javascript will connect again and start the process over. <- This is what i want to happen.

On live it will do nothing for 10 second until the script die();. Then it will show the count from 1 - 10 all at once. Then reconnect and start over.

Hosting company say it should be fine with SSE and I have also tried different kind of headers, but it makes no differens.

How to fix it or at least know the course of this?

Javascript:

var chat_sse = new EventSource("chat_sse.php", {withCredentials: true});

chat_sse.addEventListener('sse', sse_chat_toaster);
function sse_chat_toaster(event) {
   var data = event.data;
   var myDiv = document.createElement("div");
   myDiv.id = 'div_id';            
   myDiv.innerHTML = data;
   document.body.appendChild(myDiv);
};

PHP script:

<?php   
   header('Content-Type: text/event-stream');
   header('Cache-Control: no-cache');

   // header('Cache-Control: no-store, no-cache, must-revalidate');
   // header('Cache-Control: post-check=0, pre-check=0', FALSE);
   // header('Pragma: no-cache');

   // header('Accept-Encoding: gzip, deflate');
   // header('Content-Encoding: gzip');

   function chat_changes($number) {
      echo "event: sse\n";
      echo "id: chat_toaster" . PHP_EOL;
      echo "data: $number\n";
      echo PHP_EOL;   
   }

   $startedAt = time();
   $sleep = 1; // Run the loop interval in seconds
   $reset = 10; // How long before closing the connection

   $number = 1;
   
   do {
      if ((time() - $startedAt) > $reset) { die(); }

      //Check database for changes and run function
      chat_changes($number);
      $number++; //for test purpose
   
      ob_flush();
      flush();

      sleep($sleep);

   } while(true);
?>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

With some extra research i found that it was my webhosting that created trouble. Not because of SSE couldn't be done, but because they don't allow to use the functions ob_flush(); & flush(); Therefor i could not push data to the js script when i wanted and had to wait to the php script would terminate until i saw the data.

Properly because of either gzip compression or cache/varnish.

I made an Ajax long-polling solution instead.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!