PHP code example of dessimoney / event-source
1. Go to this page and download the library: Download dessimoney/event-source 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/ */
dessimoney / event-source example snippets
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();
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());