PHP code example of nerdofcode / laravel-sse

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

    

nerdofcode / laravel-sse example snippets


use LaravelSSE\Facades\SSE;

Route::get('/stream', function () {
    return SSE::create(function () {
        return ['time' => now()->toDateTimeString()];
    }, 1); // Sends event every 1 second
});

use LaravelSSE\SSE;

Route::get('/stream', function (SSE $sse) {
    return $sse->stream(function (SSE $sse) {
        $counter = 0;

        while ($sse->isConnected() && $counter < 10) {
            $sse->json(['count' => ++$counter]);
            sleep(1);
        }
    });
});

Route::get('/stream', function () {
    return app('sse')->stream(function ($sse) {
        $sse->message('Hello World!');
    });
});

SSE::stream(function (SSE $sse) {
    while ($sse->isConnected()) {
        $sse->event('data', 'event-name', 'event-id');
        sleep(1);
    }
});

SSE::create(function () {
    return ['data' => 'value'];
}, 2); // Send every 2 seconds

$sse->event('Message content', 'message', '123');

$sse->message('Hello World', '123');

$sse->json(['user' => 'John', 'status' => 'active'], 'user-update', '456');

$sse->comment('Keep-alive');

SSE::setRetry(5000)->stream(...);

SSE::setEventId('stream-1')->stream(...);

SSE::setHeader('X-Custom', 'value')->stream(...);

SSE::setExecutionTime(300)->stream(...);

while ($sse->isConnected()) {
    // Send events
}

$sse->stop();

Route::get('/counter', function () {
    return SSE::create(function () use (&$counter) {
        static $counter = 0;
        return ['count' => ++$counter];
    });
});

Route::get('/progress', function () {
    return SSE::stream(function (SSE $sse) {
        for ($i = 0; $i <= 100; $i += 10) {
            $sse->json([
                'progress' => $i,
                'status' => $i < 100 ? 'processing' : 'complete'
            ], 'progress');
            sleep(1);
        }
    });
});

Route::get('/notifications', function (Request $request) {
    $userId = $request->user()->id;

    return SSE::stream(function (SSE $sse) use ($userId) {
        $lastCheck = now();

        while ($sse->isConnected()) {
            $notifications = Notification::where('user_id', $userId)
                ->where('created_at', '>', $lastCheck)
                ->get();

            foreach ($notifications as $notification) {
                $sse->json($notification, 'notification', $notification->id);
            }

            $lastCheck = now();
            sleep(2);
        }
    });
});

return [
    'retry' => env('SSE_RETRY', 3000), // Retry time in ms
    'execution_time' => env('SSE_EXECUTION_TIME', 0), // Max execution time
];
bash
./start.sh
# Or manually: php -S localhost:8000 server.php
bash
php artisan vendor:publish --tag=sse-config