Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

Show jobs progress in dashboard

Hi, I have few jobs in my Laravel project which get's added to queue. But I was wondering if there is a way for me to display a progress bar in the dashboard of my app where customers can see if the job is still running and if failed report that?

Is this achievable using WebSockets or something?

Best regards

0 likes
2 replies
LaryAI's avatar
Level 58

To display a progress bar for queued jobs in a Laravel application, you can indeed use WebSockets to provide real-time updates to your users. Here's a step-by-step guide on how you can achieve this:

  1. Set Up Laravel Echo and Pusher:

    First, you'll need to set up Laravel Echo and a WebSocket service like Pusher. This will allow you to broadcast events from your server to your client-side application in real-time.

    • Install Laravel Echo and Pusher:

      npm install --save laravel-echo pusher-js
      
    • Configure your broadcasting.php configuration file to use Pusher:

      'pusher' => [
          'driver' => 'pusher',
          'key' => env('PUSHER_APP_KEY'),
          'secret' => env('PUSHER_APP_SECRET'),
          'app_id' => env('PUSHER_APP_ID'),
          'options' => [
              'cluster' => env('PUSHER_APP_CLUSTER'),
              'useTLS' => true,
          ],
      ],
      
    • Set your environment variables in the .env file:

      PUSHER_APP_ID=your-app-id
      PUSHER_APP_KEY=your-app-key
      PUSHER_APP_SECRET=your-app-secret
      PUSHER_APP_CLUSTER=your-app-cluster
      
  2. Create a Job and Broadcast Events:

    In your job, you can broadcast events at different stages of the job's execution to update the progress.

    • Create an event:

      php artisan make:event JobProgress
      
    • Define the event:

      namespace App\Events;
      
      use Illuminate\Broadcasting\Channel;
      use Illuminate\Queue\SerializesModels;
      use Illuminate\Broadcasting\InteractsWithSockets;
      use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
      
      class JobProgress implements ShouldBroadcast
      {
          use InteractsWithSockets, SerializesModels;
      
          public $progress;
      
          public function __construct($progress)
          {
              $this->progress = $progress;
          }
      
          public function broadcastOn()
          {
              return new Channel('job-progress');
          }
      }
      
    • Broadcast events from your job:

      namespace App\Jobs;
      
      use App\Events\JobProgress;
      use Illuminate\Bus\Queueable;
      use Illuminate\Queue\SerializesModels;
      use Illuminate\Queue\InteractsWithQueue;
      use Illuminate\Contracts\Queue\ShouldQueue;
      use Illuminate\Foundation\Bus\Dispatchable;
      
      class ExampleJob implements ShouldQueue
      {
          use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
      
          public function handle()
          {
              // Example of broadcasting progress
              for ($i = 0; $i <= 100; $i += 10) {
                  // Simulate work
                  sleep(1);
      
                  // Broadcast progress
                  broadcast(new JobProgress($i));
              }
          }
      }
      
  3. Listen for Events on the Client Side:

    Use Laravel Echo to listen for the JobProgress event and update the progress bar in your frontend.

    • Set up Laravel Echo in your JavaScript:

      import Echo from "laravel-echo";
      window.Pusher = require('pusher-js');
      
      window.Echo = new Echo({
          broadcaster: 'pusher',
          key: process.env.MIX_PUSHER_APP_KEY,
          cluster: process.env.MIX_PUSHER_APP_CLUSTER,
          forceTLS: true
      });
      
      Echo.channel('job-progress')
          .listen('JobProgress', (e) => {
              console.log('Progress:', e.progress);
              // Update your progress bar here
              document.getElementById('progress-bar').style.width = e.progress + '%';
          });
      
  4. Display the Progress Bar:

    In your HTML, you can have a simple progress bar that updates based on the events received:

    <div id="progress-container" style="width: 100%; background-color: #f3f3f3;">
        <div id="progress-bar" style="width: 0%; height: 30px; background-color: #4caf50;"></div>
    </div>
    

By following these steps, you can provide real-time feedback to your users about the progress of queued jobs in your Laravel application.

1 like
Tray2's avatar

Yes, but you need to have a query running to check the progress.

Fetch the number of records the job should process, then check periodically or dispatch events per processed record.

1 like

Please or to participate in this conversation.