Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example: Flow HTTP Component for Server-Sent Events
<?php
declare(strict_types=1);
namespace Some\Package\Http;
use Neos\Flow\Http\Component\ComponentContext;
use Neos\Flow\Http\Component\ComponentInterface;
use Neos\Flow\Http\Helper\MediaTypeHelper;
final class ServerSentComponent implements ComponentInterface
{
public function handle(ComponentContext $componentContext)
{
$request = $componentContext->getHttpRequest();
if ($request->getUri()->getPath() !== '/__ping') {
return;
}
$contentType = MediaTypeHelper::negotiateMediaType($request->getHeader('Accept'), ['text/event-stream'], true);
if ($contentType !== 'text/event-stream') {
return;
}
header('Cache-Control: no-cache');
header('Content-Type: text/event-stream');
$i = 1;
while (true) {
echo "event: ping\n";
echo 'data: ' . $i ++;
echo "\n\n";
ob_end_flush();
flush();
sleep(1);
}
}
}
Neos:
Flow:
http:
chain:
'preprocess':
chain:
'Some.Package:ServerSent':
component: 'Some\Package\Http\ServerSentComponent'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE Test</title>
</head>
<body>
<script>
const evtSource = new EventSource('/__ping');
evtSource.addEventListener('ping', function(event) {
console.log(event);
});
</script>
</body>
</html>
@bwaidelich

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.