Core\Http\Event::id
Returns the id of one server-sent event, or null when the server has not sent an id yet.
A server writes an id on an id line. The id stays in force until the server sends a new one. So an event without an id line has the id of the event before it. An id line that contains a NUL character is ignored.
The id tells your program where the stream stopped. When the connection ends, your program can connect again and tell the server the last id it received. The server then sends only the newer events. The id came from another server, so it is tainted. Check it before you send it back.
The examples below print the id of each event, show an id that stays in force, and continue a stream after the connection ends.
Return value
Returns ?tainted string. The id, tainted, or null when no id was set. The server-sent events format ignores an id line that contains a NUL byte, so the id from before that line stays.
Examples
Reading the id
<?nvs// Shows how to read the id of each event that a server sends.
// The server writes the id on an `id` line.Core\Test::answerHttp('https://bank.example.com/payments/live', 200, { body: "id: 501\ndata: 20.00 EUR received\n\nid: 502\ndata: 7.50 EUR received\n\n", headers: ['content-type' => 'text/event-stream'],});
// `id()` returns the text of the `id` line.// This prints "501: 20.00 EUR received" and then "502: 7.50 EUR received".Core\Http\Stream $payments = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://bank.example.com/payments/live');foreach ($payments->events() as Core\Http\Event $event) { echo $event->id() ?? 'no id', ': ', $event->data(), "\n";}501: 20.00 EUR received502: 7.50 EUR receivedThe id stays until changed
<?nvs// Shows that an event without an `id` line has the id of the event before it.
// The first event has no id. The second sets the id to 10.// The third has no `id` line, so its id is still 10.Core\Test::answerHttp('https://logs.example.com/tail', 200, { body: "data: starting\n\nid: 10\ndata: ready\n\ndata: first request\n\n", headers: ['content-type' => 'text/event-stream'],});
// This prints "none", then "10", then "10".Core\Http\Stream $log = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://logs.example.com/tail');foreach ($log->events() as Core\Http\Event $event) { echo $event->data(), ' -> ', $event->id() ?? 'none', "\n";}starting -> noneready -> 10first request -> 10Resuming after a disconnect
<?nvs// Shows how to continue a stream after the connection ends,// without getting the same events twice.
// The first connection ends after event 2. When a URL has `after=2`,// the server sends only the events after event 2.Core\Test::answerHttp('https://stock.example.com/changes', 200, { body: "id: 1\ndata: pencils: 40\n\nid: 2\ndata: paper: 12\n\n", headers: ['content-type' => 'text/event-stream'],});Core\Test::answerHttp('https://stock.example.com/changes?after=2', 200, { body: "id: 3\ndata: ink: 5\n\n", headers: ['content-type' => 'text/event-stream'],});
?tainted string $lastId = null;Core\Http\Stream $first = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://stock.example.com/changes');foreach ($first->events() as Core\Http\Event $event) { echo $event->data(), "\n"; // Remember the id of the last event that arrived. $lastId = $event->id();}
// The id came from the server, so it is `tainted`. `as ?int` converts it to a// whole number. So an unusual id cannot add other parameters to the URL.int $resumeAt = ($lastId as ?int) ?? 0;
// The program connects again and asks for the events after the last id.// This prints "ink: 5". The server does not send events 1 and 2 again.Core\Http\Stream $again = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://stock.example.com/changes?after=' . $resumeAt);foreach ($again->events() as Core\Http\Event $event) { echo $event->data(), "\n";}pencils: 40paper: 12ink: 5