Reactive Updates for Livewire Components Triggered by Queue Jobs
You are currently coupling a synchronous UI state to an asynchronous event flow without a defined bridge. Your component checks $product->job_status->status on every render, which works fine initially but fails to react when the status changes from processing to finished. In Laravel, simply loading a new page or reloading the browser is often too heavy for a real-time dashboard. You need a mechanism that updates the specific component instance without disrupting the rest of the application state.
The most direct approach in the Livewire ecosystem is to subscribe to database events. Since your job_statuses table is updated by the laravel-job-status package (changing the status column to finished), you can listen for that specific update event and dispatch a custom event to your component.
First, define an event in your application namespace, such as App\Events\JobFinished. This event should carry the necessary context, ideally the ID of the job status or the product itself:
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithPredispatch;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class JobFinished
{
use Dispatchable, InteractsWithPredispatch, SerializesModels;
public $jobStatus;
public function __construct($jobStatus)
{
$this->jobStatus = $jobStatus;
}
}
Next, you must listen for this event inside your application bootstrap or within the laravel-job-status package's own logic if it supports custom listeners. Assuming the package allows you to hook into its completion lifecycle (or if you are wrapping the job update logic), you would dispatch the event right after the status column is set to finished:
use App\Events\JobFinished;
// Inside your JobStatus model or a dedicated Listener
Event::dispatch(new JobFinished($jobStatus));
If you cannot modify the package's internal code, ensure there is a listener registered in App/Providers/EventServiceProvider that catches the standard database update event for the job_statuses table. You can filter by the specific column change to keep performance high:
protected $listen = [
\Illuminate\Database\Events\ModelEvent::class => [
'update' => [
function (ModelEvent $event) {
if ($event->model->getTable() === 'job_statuses' &&
$event->model->getAttribute('status') === 'finished') {
// Retrieve the related product via your foreign key relation
$product = Product::where('job_status_id', $event->model->id)->first();
if ($product) {
Event::dispatch(new JobFinished($event->model));
}
}
},
],
],
];
On the component side, you need to make it listen for JobFinished. In your ProductIndex class, use Livewire's $on('job-finished', ...) syntax. This method accepts a callback that will be executed whenever the event is dispatched:
class ProductIndex extends Component
{
public $product;
public function mount($product)
{
$this->product = $product;
}
public function render()
{
return view('livewire.merchant.product.index');
}
public function refresh()
{
// Optional: force a re-render if you need to clear any local state first
$this->resetViewData();
}
public function handleJobFinished($jobStatus)
{
// Verify the job status belongs to this specific product instance
// by comparing IDs, as the event might bubble up globally.
if ($jobStatus->id === $this->product->job_status_id) {
$this->refresh();
}
}
}
In your Blade view, you keep the conditional logic to determine whether to show the real image or the loader:
@if ($product->job_status?->status == 'finished')
<!-- Show real image -->
<img src="{{ $product->image_url }}" alt="Product Image" />
@else
<!-- Show loader -->
<div class="loader"></div>
@endif
This pattern ensures that the component only re-renders when its specific data source changes. It avoids polling the database, which is a common anti-pattern in high-traffic applications. The event-driven approach scales well because the heavy lifting of checking the queue status happens asynchronously, and the UI updates only when the result is ready.
One potential pitfall to consider is race conditions. If multiple jobs finish simultaneously, ensure your listener logic handles concurrent events gracefully. Additionally, if you are using a specific Laravel version or a custom queue driver, verify that the event dispatching does not block the worker process. The laravel-job-status package documentation might offer specific hooks for this scenario; checking their repository at https://github.com/imTigger/laravel-job-status could reveal built-in events you can subscribe to directly, simplifying the listener setup.
By decoupling the UI refresh from the job processing thread via events, you maintain a responsive application while adhering to Laravel's event-driven architecture principles.