Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

355
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda