PHP code example of marko / sse

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

    

marko / sse example snippets


use Marko\Sse\SseEvent;
use Marko\Sse\SseStream;
use Marko\Sse\StreamingResponse;

$stream = new SseStream(
    dataProvider: function () use (&$lastEventId): array {
        $messages = $this->messages->findSince($lastEventId);

        return array_map(fn ($msg) => new SseEvent(
            data: ['id' => $msg->id, 'text' => $msg->body],
            event: 'message',
            id: $msg->id,
        ), $messages);
    },
    pollInterval: 1,
    timeout: 300,
);

return new StreamingResponse($stream);