PHP code example of maniaba / codeigniter4-sse

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

    

maniaba / codeigniter4-sse example snippets


sse()->publish(new OrderPaidNotification($userId, 918));

namespace App\Sse\Channels;

use Maniaba\CodeIgniterSse\Authorization\ChannelAuthorizationContext;
use Maniaba\CodeIgniterSse\Contracts\ChannelDefinitionInterface;
use Maniaba\CodeIgniterSse\Support\Channel;

final class UserNotificationsChannel implements ChannelDefinitionInterface
{
    public static function pattern(): string
    {
        return 'users.{userId}.notifications';
    }

    public static function forUser(int|string $userId): Channel
    {
        return Channel::join('users', $userId, 'notifications');
    }

    public function authorize(ChannelAuthorizationContext $context): bool
    {
        $user = $context->user();

        return $user !== null
            && (string) $user->id === $context->param('userId');
    }
}

public array $channels = [
    \Maniaba\CodeIgniterSse\Authorization\Channels\PublicChannel::class,
    \App\Sse\Channels\UserNotificationsChannel::class,
];

public string $userResolver = \App\Sse\UserResolver::class;



declare(strict_types=1);

namespace Config;

use Maniaba\CodeIgniterSse\Config\Sse as BaseSse;

final class Sse extends BaseSse
{
    public string $channelPrefix = 'myapp:sse:';
    public int $heartbeatInterval = 15;
    public int $maxConnectionSeconds = 300;
    public int $maxChannelsPerConnection = 20;
}

public string $broker = 'mercure';
bash
redis-cli ping
php spark sse:health-check