Search code examples
phplaravel-5real-time

Laravel 5 progress bar to see status of runing command in browser


I've question about Laravel 5: is it real to do real-time page, where I can run laravel command and display on that page progress of that command?

For example: we have search index with 60k rows and we need to rebuild that index, in the laravel command we can do it with cycle and in the edn of each iteration we can show progress bar in the command line.

But how to show it in browser page? Is there some packages for laravel 5? Or others solutions?

Maybe someone knows some solutions how to retrieve answer from command and don't stop it, so command will run async?

Thanks.


Solution

  • Simplest way I can think of doing it is to store the command progress in a cache like memcache or redis and then have the front-end query that cache key for the progress.

    Command/Backend:

    $cache = new Memcache;
    $cache->addServer('localhost', 'port');
    
    foreach ($records as $key => $index) {
        $your_index->index($index);
        $cache->set('command_progress', ($key / count($records)) * 100);
    }
    

    Frontend:

    $cache = new Memcache;
    $cache->addServer('localhost', 'port');
    
    $percentage = $cache->get('command_progress') ? : 0;
    var_dump($percentage);
    

    Put the frontend code in an ajax script and keep polling it till it reaches 100%.