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

278
Vistas
Server Sent Events (Laravel) not sending my content to client side

I am trying to send updates to client side using SSE, but when I trigger the event in the server side, the new data is not sent to client side.

My client side code:

<script>
  $(document).ready(function($) {

     var lastMsg = '';
     const source = new EventSource("/misc/getData");
     source.onmessage = function(event) {
        console.log(event);
        var data = JSON.parse(event.data);
        if (data !== lastMsg) {
           $('#result').empty();
           $('#result').append('Result: ' + data);
           lastMsg = data;
        }
     };
 });
 </script>

My server side code (Laravel class)

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class MiscController extends Controller {

   private $data;

   public function __construct() {
      $this->data = '';
   }


   public function sendContent(Request $request) {
      $content = $request['content'];
      if (!is_null($content)) {
         $this->data = "data:". $content ."\n\n";
      }
      $this->getData();
   }


   public function getData() {
      return response($this->data)->withHeaders(['Content-Type'=>'text/event-stream', 'Cache-Control'=>'no-cache']);
   }

}

No errors are shown, headers look ok in the browser console but I am not able to get the data when it is set by the server side.
The sendContent method is called on the server side by other classes or from the client side using AJAX, to set the content that needs to be reflected on the UI.

However, the onmessage function on the client side never gets it.

I also tried using a static method receiving the content as parameter, but to no avail.

What am I missing?

about 4 years ago · Juan Pablo Isaza
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