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

362
Views
js + php : Why did the EventSource can't get any responses?

I have a stream response like following:

I'm using Laravel as backend

public function stream() {
    return response()->stream(function() {
        while (true) {

            if (connection_aborted()) {
                Log::debug('aborted');
                break;
            }
            
            echo "\nevent: process\n", 'data: {"time": ' . time() . '}', "\n\n";
            Log::debug('echoed');

            ob_flush();
            flush();

            sleep(3);
        }
    }, 200, [
        'Cache-Control' => 'no-cache',
        'Content-Type' => 'text/event-stream',
    ]);
}

frontend:

let stream = new EventSource(`http://example.test/api/stream`);
stream.addEventListener('process', event => {
    console.log(event.data)
});

as the code show, it's expected to console.log(event.data) every 3 seconds.

Although I can get the echoed Logs (but not the aborted) in backend, but I can't get any output in the console of browser.

I've googled but nothing helps. How did this error occur and how can it be solved?

Thanks a lot for any replys!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just add a response header

'X-Accel-Buffering' => 'no'

Setting this to “no” will allow unbuffered responses suitable for Comet and HTTP streaming applications. Setting this to “yes” will allow the response to be cached.

Refrences:

  1. x-accel-buffering
  2. Event Stream - Server-Send Events
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!