GitHub

Poller

A poller requests a URL again and again and applies every response, like any other. The server is in control: each request sends the id of the poller, so a response can change its interval, mute it or stop it.
  • new Poller($url, $interval)

    Starts with the page, or with the pagelet whose content prints it.

  • Poller::requestedId()

    The poller that sent the request, to control it from the response.

  • setInterval, mute, stop

    Called on the poller by the response.

  • setMaxRequests(), setMuteWhenIdle()

    Limits, and a pause while the user is away or the tab is hidden.

Controller.php
// The pagelet of the deploy starts the poller with itself
(new Poller(route('deploy.status'), 2000))
    ->setMaxRequests(20)
    ->start();

// The endpoint of the poller
$response = (new AsyncResponse())
    ->setContent('#deploy-progress', view('progress', compact('progress'))->render());

if ($progress >= 100) {
    $response->call(Poller::requestedId(), 'stop');
}

return $response->send();
Try it

main · 4 steps · about 8 s

Deploy

A deploy that reports its progress

Start it: the response is a pagelet that starts a poller. Every 2 s the poller asks for the progress, and the server stops it when the deploy is done.