Cedric Leblond
Cedric Leblond

Reputation: 1

Data in browser received from Server Sent Event are buffered in 8192 bytes packets

I'm developing a web application in which I'm using Server Sent Event to update a web page. When testing on localhost everything is running fine. However when used online (OVH cloud provider) the output of my data are truncated at 8192 bytes. I have to wait tens of seconds or sometimes few minutes before the complete data can be received and displayed.

In the browser console network tab the eventstream part is empty but the response part show me the information I request but it's truncated. Eventstream tab JSON response truncated

Is there anyone of you who already faced the same issue ? Thanks for your appreciated help.

Here is the front code:

if (typeof(EventSource) !== 'undefined') {
            console.info('Starting connection...');

            var source = new EventSource('compet/updateSSETables/' + activeTournoi[0]['idTournoi']);

            source.addEventListener('open', function(e) {
                console.info('Connection was opened.');
            }, false);

            source.addEventListener('error', function(e) {
                var txt;
                switch (e.target.readyState) {
                    // if reconnecting
                    case EventSource.CONNECTING:
                        txt = 'Reconnecting...';
                        break;
                    // if error was fatal
                    case EventSource.CLOSED:
                        txt = 'Connection failed. Will not retry.';
                        break;
                }
                console.error(e);
            }, false);

            source.addEventListener('message', function(e) {
                if(e.origin == ajaxURL) {
                    table = JSON.parse(e.data);
                    refreshTableau(table['enCours'], function(){
                        $('.scroll').verticalLoop();
                    });
                }
            }, false);

        } else {
            alert('Your browser does not support Server-sent events! Please upgrade it!');
            console.error('Connection aborted');
        }

Here is the back code :

public function updateSSETables($idTournoi)
    {
        // variables
        $dateLU = 0;
        // Make session read-only
        session_write_close();
        // disable difault disconnect check
        ignore_user_abort(true);

        header('Content-Type: text/event-stream');
        header('Cache-Control: no-cache');
        header('Connection: keep-alive');
        header('X-Accel-Buffering: no');

        ob_implicit_flush(true);

        while(!connection_aborted())
        {
            $currentDate = date("r");
            // récupère l'heure de dernière modif du tournoi
            $tournoi = $this->tournois_mo->getTournoi($idTournoi);

            // Si la date prédécemment retenue est inférieur à la date de dernière modif du tournoi
            if($dateLU < strtotime($tournoi[0]->LastUpdateTournoi))
            {
                // JE retiens la nouvelle heure
                $dateLU = strtotime($tournoi[0]->LastUpdateTournoi);
                // Recupération du statut des tables
                
                $matchSSE['enCours'] = $this->getTabMatchsEnCours($idTournoi, $tournoi[0]->nbTables);
                $matchSSE['enAttente'] = $this->getTabMatchsEnAttente($idTournoi, $tournoi[0]->nbTables);
                // J'envoie les infos
                header('Content-Length: 20000');
                echo "event: message\n";
                echo "data: ".json_encode($matchSSE)."\n\n";                                   
            }
            else
            {
                
                echo ": heartbeat ".$currentDate." \n\n";
            }
            // envoie les données
            ob_end_flush();
            flush();  
            // Attent 1 seconde
            sleep(1);
        }
    }

I already tried to use ob_flush, ob_end_flush, ob_implicit_flush, etc. but didn't change anything. It seems that packets are sent by 8192 bytes and as long as there's no 8192 bytes, packets are not sent.

Upvotes: 0

Views: 82

Answers (0)

Related Questions