This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/lib/App/SSE/Sender.php
2022-03-23 10:08:36 +01:00

35 lines
796 B
PHP

<?php
namespace App\SSE;
/**
* Small class to abstract Server-Sent Events (SSE)
*
* https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
*/
class Sender
{
public function __construct()
{
header("Cache-Control: no-store");
header("Content-Type: text/event-stream");
// as session data is locked to prevent concurrent writes we
// make it read only to prevent the server from locking up
session_write_close();
}
public function send($data, $event = NULL): void
{
if ($event)
{
echo "event: $event\n";
}
echo "data: " . json_encode($data) ."";
echo "\n\n";
// send data to stream
ob_end_flush();
flush();
}
}