PHP code example of roiwk / sse-client

1. Go to this page and download the library: Download roiwk/sse-client 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/ */

    

roiwk / sse-client example snippets




$client = new Roiwk\SSEClient\Client('http://127.0.0.1:8888');
$client->addEventListener('ping', function ($data) {
    echo "Received ping event: $data\n";
});
$client->start();

// construct usage
$client = new Roiwk\SSEClient\Client('http://127.0.0.1:8888', [
    'retryInterval' => 3,
    'onmessage' => function(string $data){},
    'streamContextOptions' => [
        'http' => [
            'method' => 'POST',
            'header' => [
                'Content-Type: application/json',
            ],
            'content' => json_encode(['test' => 1]),
        ],
    ],
]);

// method usage
$client->addEventListener('ping', function ($data) {
    echo "Received ping event: $data\n";
});

$client->onmessage(function(string $data) use ($client){
    echo $data.PHP_EOL;
    $iWantClose = true;
    if ($iWantClose) {
        $client->close();
    }
});

$client->setRetryInterval(1);

$client->start();

sh
php -S 127.0.0.1 server.php