eliashaeussler / sse
PHP implementation of server-sent events using event streams
Installs: 87 810
Dependents: 1
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- php-http/discovery: ^1.18
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- armin/editorconfig-cli: ^1.6
- eliashaeussler/php-cs-fixer-config: ^1.2
- eliashaeussler/phpstan-config: ^2.1
- eliashaeussler/rector-config: ^1.6
- ergebnis/composer-normalize: ^2.31
- nyholm/psr7: ^1.8
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-symfony: ^1.3
- phpunit/phpunit: ^10.2
This package is auto-updated.
Last update: 2024-11-21 20:42:28 UTC
README
Server-Sent Events in PHP
A server implementation of server-sent events, written in PHP. It can be used to stream events back to the client. All event-driven features are highly customizable, even custom emitters can be used. The library also provides a PSR-7 compliant event stream implementation.
π Features
- Server implementation of server-sent events (SSE)
- Highly customizable via interface implementations
- Self-emitting event stream
- PSR-7 compliant event stream
π₯ Installation
composer require eliashaeussler/sse
β‘ Usage
use EliasHaeussler\SSE; // Open event stream $eventStream = SSE\Stream\SelfEmittingEventStream::create(); $eventStream->open(); // Send event $eventStream->sendEvent(new MyCustomEvent($eventData)); // Send message $eventStream->sendMessage('myCustomEvent', $eventData); // Close event stream $eventStream->close();
π’ Architecture
Event stream
All events are sent through event streams. The library provides an
interface Stream\EventStream
that abstracts
all stream parts during the whole event streaming progress.
The following event stream implementations are currently available:
Stream\SelfEmittingEventStream
sends all stream data to an emitter that takes care of writing data into an active resource. This is the best working solution in most cases.Stream\Psr7EventStream
creates a PSR-7 compliant response during the whole event streaming progress. Note that this response is not emitted on real time.
Emitter
All data of a self-emitting event stream is handed over to a given emitter.
The library provides an interface Stream\Emitter\Emitter
that takes care of all passed data.
The following emitter implementations are currently available:
Stream\Emitter\RealtimeEmitter
writes all stream data directly to the active standard output.
Event
There are two ways how data can be sent through an active event stream:
- By sending a prebuilt event by calling
Stream\EventStream::sendEvent()
. - By sending a plain message by calling
Stream\EventStream::sendMessage()
.
Events can be generated by implementing the Event\Event
interface. Each event must provide an event name and JSON-serializable
event data.
π§βπ» Contributing
Please have a look at CONTRIBUTING.md
.
β License
This project is licensed under GNU General Public License 3.0 (or later).