Core\Http\Event::name
Returns the name of one server-sent event, or null when the event has no name.
A server can give each event a name on an event line, for example order.created or ping. Your program uses the name to decide what to do with the event. The name belongs to one event only. The next event has no name unless it has its own event line. An event without a name is what the format calls a message, so $event->name() ?? "message" gives the name in every case.
The name came from another server, so it is tainted, like the data.
The examples below print the name of each event, show an event without a name, and handle each kind of event from a build server in its own way.
Return value
Returns ?tainted string. The name, tainted. It is null when the event has no event line. The format calls such an event a message.
Examples
Reading event names
<?nvs// Shows how to read the name of each event that a server sends.
// The server writes the name on an `event` line, before the data.Core\Test::answerHttp('https://shop.example.com/orders/live', 200, { body: "event: order.created\ndata: 1042\n\nevent: order.paid\ndata: 1042\n\n", headers: ['content-type' => 'text/event-stream'],});
// `name()` returns the text of the `event` line.// This prints "order.created 1042" and then "order.paid 1042".Core\Http\Stream $orders = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://shop.example.com/orders/live');foreach ($orders->events() as Core\Http\Event $event) { echo $event->name() ?? 'message', ' ', $event->data(), "\n";}order.created 1042order.paid 1042Events without a name
<?nvs// Shows what `name()` returns for an event that has no `event` line.
// Only the first event has a name. A name belongs to one event only.Core\Test::answerHttp('https://chat.example.com/room/7', 200, { body: "event: joined\ndata: Anna\n\ndata: Hello everyone\n\n", headers: ['content-type' => 'text/event-stream'],});
// For the second event, `name()` returns `null`.// This prints "joined: Anna" and then "no name: Hello everyone".Core\Http\Stream $room = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://chat.example.com/room/7');foreach ($room->events() as Core\Http\Event $event) { if ($event->name() == null) { echo 'no name: ', $event->data(), "\n"; } else { echo $event->name(), ': ', $event->data(), "\n"; }}joined: Annano name: Hello everyoneHandling each kind of event
<?nvs// Shows how to handle each kind of event in its own way,// as a program does when it follows the status of a build.
// The server sends three kinds of events. It also sends `ping` events,// which only show that the connection is still open.Core\Test::answerHttp('https://ci.example.com/builds/88/events', 200, { body: "event: step\ndata: compile\n\nevent: ping\ndata: -\n\nevent: step\ndata: test\n\nevent: finished\ndata: success\n\n", headers: ['content-type' => 'text/event-stream'],});
Core\Http\Stream $build = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://ci.example.com/builds/88/events');int $steps = 0;foreach ($build->events() as Core\Http\Event $event) { ?tainted string $kind = $event->name(); if ($kind == 'step') { $steps = $steps + 1; echo 'running: ', $event->data(), "\n"; } elseif ($kind == 'finished') { echo 'result: ', $event->data(), ' after ', $steps, " steps\n"; } // Every other kind, like `ping`, is skipped.}running: compilerunning: testresult: success after 2 steps