Core\Response\Stream::write
Sends one part of a response body that Core\Response::startStream started. The part is a string or bytes, and it is sent exactly as it is. An empty part sends nothing and is not an error.
If the client has not finished reading the last part, write waits until it has. So only one part waits in memory at a time, even when the client reads slowly. If the client closes the connection, or stops reading for longer than the server's send timeout, write returns at once and the part is not sent. Your program still runs to its end. After end(), write throws a LogicError.
The client gets one body. It does not see where one part ends and the next part starts.
The examples show a string and bytes, parts of different sizes, and a list sent as one JSON object on each line.
Parameters
$chunkstring|bytesThe part to send, as astringor asbytes. It is sent exactly as it is. An empty part sends nothing and is not an error. Ataintedvalue does not compile, because the browser may read the part as HTML.
Return value
Returns void. Nothing. When this method returns, the part has been given to the connection. If the client has gone, this method returns at once and the part is thrown away. The client has gone when it closed the connection, or when it did not read a part before the server's send timeout. Your request still runs to its end.
Errors
RuntimeErrorThe part is larger than the server allows for one part. Nothing is sent, and you can still send smaller parts.LogicErrorThe stream was started with a$length, and this part goes past the end oflength(). Nothing is sent. It is also thrown whenend()was already called on the stream.
Examples
Text and bytes
<?nvs// Shows that `write` sends a `string` or `bytes`.
Core\Response\Stream $body = Core\Response::startStream('text/plain; charset=utf-8');
// A `string` is sent exactly as it is.$body->write("Hello from a string.\n");
// `bytes` is sent exactly as it is too. `as bytes` converts the text to `bytes`.$body->write("Hello from bytes.\n" as bytes);Hello from a string.Hello from bytes.Parts of any size
<?nvs// Shows that the client gets one body, whatever the size of each part.
Core\Response\Stream $body = Core\Response::startStream('text/plain; charset=utf-8');
// These three parts arrive as one line. This prints "Hello, world".$body->write('Hel');$body->write('lo, wor');$body->write("ld\n");
// An empty part sends nothing, and it is not an error.$body->write('');$body->write("Done.\n");Hello, worldDone.One json object per line
<?nvs// Shows how an API sends a list as JSON Lines, which is one JSON object on each line.
array<string> $names = ['Green tea', 'Coffee', 'Cocoa'];
// The client can read each line as soon as it arrives.// It does not need to wait for the whole list.Core\Response\Stream $lines = Core\Response::startStream('application/x-ndjson');uint $id = 0;foreach ($names as string $name) { $id = $id + 1; // `Core\Json::encode` writes the object on one line. $lines->write(Core\Json::encode({ id: $id, name: $name }) . "\n");}{"id":1,"name":"Green tea"}{"id":2,"name":"Coffee"}{"id":3,"name":"Cocoa"}