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

39 lines
886 B
PHP
Raw Normal View History

2022-03-23 09:08:36 +00:00
<?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
{
2022-03-23 13:03:33 +00:00
public function start(): void
2022-03-23 09:08:36 +00:00
{
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();
2022-03-23 13:03:33 +00:00
// we have to flush before because idk
ob_end_flush();
flush();
2022-03-23 09:08:36 +00:00
}
2022-03-23 13:03:33 +00:00
public function flush($data, $event = NULL): void
2022-03-23 09:08:36 +00:00
{
if ($event)
{
echo "event: $event\n";
}
echo "data: " . json_encode($data) ."";
echo "\n\n";
// send data to stream
ob_end_flush();
flush();
}
}