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

354
Views
js + php: ¿Por qué EventSource no puede obtener ninguna respuesta?

Tengo una respuesta de flujo como la siguiente:

Estoy usando Laravel como 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', ]); }

Interfaz:

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

como muestra el código, se espera que console.log(event.data) cada 3 segundos.

Aunque puedo obtener los registros echoed (pero no los aborted ) en el backend, no puedo obtener ningún resultado en la consola del navegador.

He buscado en Google pero nada ayuda. ¿Cómo ocurrió este error y cómo se puede solucionar?

¡Muchas gracias por cualquier respuesta!

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

0

Simplemente agregue un encabezado de respuesta

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

Establecer esto en "no" permitirá respuestas sin búfer adecuadas para las aplicaciones de transmisión Comet y HTTP. Establecer esto en "sí" permitirá que la respuesta se almacene en caché.

Referencias:

  1. x-accel-búfer
  2. Flujo de eventos - Servidor-Enviar eventos
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!