Method
ServerSentEvents::comment
Send a comment: the browser ignores it, so it keeps the stream alive through proxies and lets a handler find out that the client is gone.
Description#
public ServerSentEvents::comment(
string $text = ''
): voidParameters#
- $text string = ''
- What the comment says; line breaks become spaces
Errors / Exceptions#
| Exception | When |
|---|---|
IOException | When the client is gone |
Examples#
use Swerve\ClientRequest;
use Swerve\RequestHandler;
use Swerve\ServerSentEvents;
$app = new RequestHandler(function (ClientRequest $request) {
$events = new ServerSentEvents($request);
for ($i = 0; $i < 2; ++$i) {
phasync::sleep(0.05); // in production: some seconds without news
$events->comment('keep-alive'); // the browser ignores it; a client that left makes it throw
}
$events->send('news at last');
});
if (debug_backtrace()) {
return $app; // swerve loads this file: it is the application's swerve.php
}
// Run directly: serve the application with swerve, and send it requests
require __DIR__ . '/../serve.php';
serve(__FILE__, ["GET /news HTTP/1.1\r\nHost: example\r\n\r\n"]);Output
HTTP/1.1 200 OK content-type: text/event-stream cache-control: no-cache x-accel-buffering: no Server: Swerve Transfer-Encoding: chunked Connection: close e : keep-alive e : keep-alive 14 data: news at last 0