Skip to content

Core\Http\Event::data

$event->data(): tainted string

Returns the data of one server-sent event, as a string.

A server that sends events writes the data of each event on one or more data lines. data() joins those lines with a line break and returns the result. An event always has data. A block without a data line is not an event, so a foreach over Core\Http\Stream::events() does not give it to your program.

The data came from another server, so it is tainted. You can print it, compare it and join it with other text. Check it before you use it in a place that needs trusted text.

The examples below print the data of each event, show an event with three data lines, and build one long answer from the small pieces a text generation service sends.

Return value

Returns tainted string. The data, tainted like every byte of a reply. An event with no data line is not given to the loop at all.

Examples

Reading the data of each event

<?nvs
// Shows how to read the data of each event that a server sends.
// The server's reply is fixed here, so the example runs without a network.
// Each event ends with an empty line.
Core\Test::answerHttp('https://news.example.com/live', 200, {
body: "data: Match started\n\ndata: Goal for the home team\n\n",
headers: ['content-type' => 'text/event-stream'],
});
// `data()` returns the text of one event. This prints two lines.
Core\Http\Stream $feed = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://news.example.com/live');
foreach ($feed->events() as Core\Http\Event $event) {
echo $event->data(), "\n";
}
Output
Match started
Goal for the home team

Data on several lines

<?nvs
// Shows what `data()` returns when one event has several `data` lines.
// The first event has three `data` lines. The second event has one.
Core\Test::answerHttp('https://weather.example.com/alerts', 200, {
body: "data: Storm warning\ndata: Area: north coast\ndata: Until: 18:00\n\ndata: All clear\n\n",
headers: ['content-type' => 'text/event-stream'],
});
// `data()` joins the lines of one event with a line break.
// This prints the three lines of the first event, then "All clear".
Core\Http\Stream $alerts = Core\Http\Client::openStream(Core\Http\Method::Get, 'https://weather.example.com/alerts');
int $number = 1;
foreach ($alerts->events() as Core\Http\Event $alert) {
echo 'Alert ', $number, ":\n", $alert->data(), "\n";
$number = $number + 1;
}
Output
Alert 1:
Storm warning
Area: north coast
Until: 18:00
Alert 2:
All clear

Joining a streamed answer

<?nvs
// Shows how to build a long answer that a server sends in small pieces,
// as a text generation service does.
// Each event has one piece of the answer. The last event is "[DONE]".
Core\Test::answerHttp('https://ai.example.com/v1/answer', 200, {
body: "data: The order\n\ndata: ships\n\ndata: on Monday.\n\ndata: [DONE]\n\n",
headers: ['content-type' => 'text/event-stream'],
});
Core\Http\Stream $reply = Core\Http\Client::openStream(Core\Http\Method::Post, 'https://ai.example.com/v1/answer');
tainted string $answer = '';
foreach ($reply->events() as Core\Http\Event $event) {
// "[DONE]" is not part of the answer. It says that the answer is complete.
if ($event->data() == '[DONE]') {
break;
}
$answer = $answer . $event->data();
}
// This prints "The order ships on Monday."
echo $answer, "\n";
Output
The order ships on Monday.