Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

349
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda