dimanche 19 février 2023

Laravel SSE start sending the events after long time

I am integrating the SSE with laravel 5.7 on a server in php 7.4 (prod&local) but locally I receive the events without problem, unfortunately the client can wait up to 10 minutes to start receiving the events. And especially on the server side according to my logs, it starts sending the events as soon as the connection is opened, I wonder what could be blocking (or buffering) the results for sending them to after some time what can crash my server in prod.

Controller

public function index()
{
    return response()->stream(function () {
            try{
                while(true){
                    Log::info("in".now());
                    echo "data: " . now() . "\n\n";
                    ob_flush();
                    flush();
                    Log::info("out".now());
                    sleep(3);
                }
                                    
            }catch(\Exception $e){
                Log::error($e->getMessage());
            }
            if (connection_aborted()) {break;}


    }, 200, [
        'Content-Type' => 'text/event-stream',
        'Cache-Control' => 'no-cache',
        'Connection' => 'keep-alive'
    ]);
}

Javascript

<script>
  window.onload= function(){
    console.log('loaded')
  var source = new EventSource('');
  source.addEventListener("message", function(event) {
    const trans = event.data;
    console.log(Date(),trans)

  });
  source.onerror = (e)=>{
    console.log(e)
  }
</script>


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/zj2StXb
via IFTTT

Aucun commentaire:

Enregistrer un commentaire