OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter jgarcias
  • Start date Start date
J

jgarcias

Guest
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:

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)

Code:
<?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?

<p>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.</p>
<p>My client side code:</p>
<pre><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>
</code></pre>
<p>My server side code (Laravel class)</p>
<pre><code><?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']);
}

}
</code></pre>
<p>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.<br>
The <code>sendContent</code> 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.</p>
<p>However, the <code>onmessage</code> function on the client side never gets it.</p>
<p>I also tried using a static method receiving the content as parameter, but to no avail.</p>
<p>What am I missing?</p>
 
Top