Method
ServerSentEvents::lastEventId
The Last-Event-ID the client sent when it reconnected: the id of the last event it saw before the connection dropped, or null at its first connection.
Description#
public ServerSentEvents::lastEventId(): ?stringReturn values#
?string
Examples#
use Swerve\ClientRequest;
use Swerve\RequestHandler;
use Swerve\ServerSentEvents;
$app = new RequestHandler(function (ClientRequest $request) {
$news = [1 => 'first', 2 => 'second', 3 => 'third'];
$events = new ServerSentEvents($request);
$after = (int) ($events->lastEventId() ?? 0); // a reconnecting browser says what it saw last
foreach ($news as $id => $item) {
if ($id > $after) {
$events->send($item, id: (string) $id);
}
}
});
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\nLast-Event-ID: 1\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 14 id: 2 data: second 13 id: 3 data: third 0