hhxsv5 / php-sse

A simple and efficient library implemented HTML5's server-sent events by PHP, is used to real-time push events from server to client, and easier than Websocket, instead of AJAX request.
MIT License
426 stars 49 forks source link

SSE creates new connection #23

Open ljfreelancer88 opened 3 years ago

ljfreelancer88 commented 3 years ago

I'm wondering the script below that I grabbed from Swoole website creates new connection or thread every couple of seconds and I don't like it. While yours is not which is I'm looking for. Sorry but this is not really an issue. Just want to ask or mentor me what's the idea to prevent SSE from creating new connection every couple of seconds. I want SSE to maintain that connection until that client closes or refresh the browser. Thank you in advance.

while(true):
        $data = "event: ping\n";
        $response->write($data);
        $curDate = date(DATE_ISO8601);
        $data = 'data: {"time": "' . $curDate . '"}';
        $data .= "\n\n";
        $response->write($data);
        $counter--;
        if (!$counter) {
            $data = 'data: This is a message at time ' . $curDate . "\n\n";
            $response->end($data);
            break;
        }
        Coroutine::sleep(3);
    endwhile;

Screenshot from 2021-03-02 08-18-35