dessimoney / event-source
Implementation of Event Source API (Server-Sent Events https://www.w3.org/TR/eventsource)
1.0.8
2025-04-08 10:35 UTC
Requires
- php: >=8.1
- ext-json: *
- symfony/http-foundation: ^7
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
README
PHP Event Source is a simple library for handle Event Source API through HTTP protocol, followed specifics at https://www.w3.org/TR/eventsource.
Getting started
Requirements
php >= 7.1
Installation
composer require dessimoney/event-source
How to use
Default usage
use EventSource\EventSender; use EventSource\EventBufferInterface; use EventSource\Event; $sender = new EventSender(); // Create new sender instance // Configure sender adding listeners $sender->addStartListener( function () { // What do you want when I'm starting? } ); $sender->addWriteListener( function (EventBufferInterface $buffer) { $event = new Event('ping', 'ping at: ' . time()); $buffer->write($event); } ); $sender->addStopListener( function () { // What do you want when I'm stopping? } ); $sender->send();
Custom usage
use EventSource\EventBufferInterface; use EventSource\Event; use EventSource\EventSender; // If you want to use a custom buffer you can extend \EventSource\EventBufferInterface class MyOwnBuffer implements EventBufferInterface { public function write(Event $event) : void { echo 'MyOwnBuffer write this'; } } // And the set to EventSender instance $sender = new EventSender(); $sender->setBuffer(new MyOwnBuffer());
License
Built under MIT license.
Authors and Copyright
Lorenzo Dessimoni - lorenzo.dessimoni@gmail.com