PHP code example of exinfinite / server-event

1. Go to this page and download the library: Download exinfinite/server-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

exinfinite / server-event example snippets


composer 

header("Cache-Control: no-cache");
header("Content-Type: text/event-stream");
use Exinfinite\ServerEvent;
$count = 1;
$interval = 30;
ServerEvent::setEvent('userconnect');//event type
while (true) {
    ServerEvent::setId($count);//event id
    ServerEvent::shot("Server Sent Event round {$count}");//event data
    if($count == '5'){
        ServerEvent::shot("connection closed");
        ServerEvent::close();//關閉連線
    }
    $count++;
    sleep($interval);
}