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 14:03:33 +01:00

39 lines
886 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 start(): void
{
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();
// we have to flush before because idk
ob_end_flush();
flush();
}
public function flush($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();
}
}