sovich/simple-server-sent-event

There is no license information available for the latest version (2.2.0) of this package.

Simple "Server Sent Events" based on cache

2.2.0 2024-09-20 20:00 UTC

This package is auto-updated.

Last update: 2024-10-21 08:21:22 UTC


README

Symfony Controller

use Psr\Cache\CacheItemPoolInterface;
use SSE\Response\SSEResponse;
use SSE\Repository\EventStoreRepository;
use SSE\SSEEventListenerFactory;
use Symfony\Component\Routing\Annotation\Route;

 #[Route(path: '/listener')]
final class TransactionSSEController
{
    public function __invoke(CacheItemPoolInterface $cacheItemPool): SSEResponse
    {
        $eventStoreRepository = new EventStoreRepository($cacheItemPool);
        $factory = new SSEEventListenerFactory($eventStoreRepository);
        $sse = $factory->create(
            events: ['eventName'],
            maxTimeoutWithoutResponse: 60,
            pingInterval: 15,
            maxExecutionTime: 300,
        );

        return new SSEResponse($sse, 500000);
    }
}

Push event

use SSE\Event\Event;
use SSE\Repository\EventStoreRepository;
use SSE\SSEEventDispatcher;

$eventStoreRepository = new EventStoreRepository($cacheItemPool);
$event = new Event(uniqid(), 'eventName', 'Patient update');

(new SSEEventDispatcher($eventStoreRepository))->dispatch($event);