class EventStream

A response that reaches the browser as it's written, for work that takes a while and has something to show along the way: a step finished, a line of output, or a reply being written.

The body is newline-delimited JSON, one event per line. Each event has its `type`, whatever the callback gives with it, and `ms`, the milliseconds since the stream began. In the browser, `flarum/common/utils/readEventStream`, a chunk loaded when it's first imported, hands over each event as its line arrives. public function handle(ServerRequestInterface $request): ResponseInterface { RequestUtil::getActor($request)->assertAdmin(); return $this->events->response(function (Closure $send) { foreach ($this->importer->batches() as $batch) { $send('imported', ['count' => $this->importer->import($batch)]); } $send('done'); }); } The callback runs once the middleware has passed the request, as the response is sent ({\Flarum\Http\Emitter}). By then the status has been sent too, so a failure can't be an error response: send it as an event of its own, and end with an event saying the work is done, as a dropped connection looks just like a finished one. An exception the callback doesn't catch is reported like any other, and sent as `{"type": "error"}`. Proxies are asked not to hold the response back, and PHP doesn't, but a server can still be set up to: the admin dashboard warns when it is.

Methods

__construct( Container $container)

No description

ResponseInterface
response( Closure $callback)

No description

void
run( Closure $callback)

No description

Details

__construct( Container $container)

Parameters

Container $container

ResponseInterface response( Closure $callback)

Parameters

Closure $callback

Return Value

ResponseInterface

protected void run( Closure $callback)

Parameters

Closure $callback

Return Value

void