I want to send some sort of (unique, auto-incrementing) number as part of the payload of an event - so that the consumer can, for example, know it should ignore an 'updated' event if the event is older than a previous 'update' event it received.
I see I can add a broadcastWith
method to my event, where I could add such a number, which I'm storing in some table.
But, I don't really need to create a new number. The ID of the job in the jobs
table will work just fine. So, how can I make Laravel automatically add a property, say order
, to this event before it is broadcast and make the value of order
to id
column from the jobs
table? Is there a way to get it in the broadcastWith
method?
I had previously thought of using a timestamp as the 'order' but of course that won't help me or the consumer when two events have been created in a short-a timeframe as a computer can create two events.
CodePudding user response:
Hi such a nice requirement, i have been working on a POCO and here are some snippets, you do not need broadcast at all. Of course you need to have the queue worker up and running Running The Queue Worker
On your order controller, i guess you need the update method to dispatch the job before commiting.
function update(Request $req) { $data= Order::find($req->id); $data->amount=$req->amount; //example field PostUpdateOrderJob::dispatch($data)->beforeCommit(); $data->save(); }
Important: Why before commit and not after commit: Setting the after_commit configuration option to true will also cause any queued event listeners, mailables, notifications, and broadcast events to be dispatched after all open database transactions have been committed.
Your PostUpdateOrderJob class
?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use App\Models\Order; use Throwable; class PostUpdateOrderJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * The number of times the job may be attempted. * * @var int */ public $tries = 25; /** * The maximum number of unhandled exceptions to allow before failing. * * @var int */ public $maxExceptions = 3; protected $order; /** * Create a new job instance. * * @return void */ public function __construct(Order $order) { $this->order=$order; } /** * Execute the job. * * @return void */ public function handle() { $this->order->update(['jobid'=>$this->job->getJobId()]); } public function failed(Throwable $exception) { // Send user notification of failure, etc... //Several options on link below //https://laravel.com/docs/9.x/queues#dealing-with-failed-jobs } }
CodePudding user response:
Well php has a function called time()
which returns the current unix time in seconds, so you can just use that in your broadcast event.
In your broadcast event, you can add this time
to the public class properties, which would then be available through the event payload:
class MyBroadcastEvent implements ShouldBroadcast
{
public $time;
public function __construct()
{
$this->time = time();
}
}
I'm not sure if this is exactly what you need, your question is kind of confusing to be honest.