in Laravel, you can use jobs to execute tasks in a back-end queue while the rest of the application does other things. i have a job that is initiated by user input. immediately, through javascript, i give the user a notification that the job is being processed.
i would like to be able to give a similar notification after the job has successfully completed.
i am calling my job from within a model like this:
public function doSomething() {
$job = new \App\Jobs\MyJob();
app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($job);
}
and this is how my job headers look like:
class MyJob extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels, Queueable;
...
}
the model job call is actually triggered from a controller method:
public function getDoSomething($id) {
$item = Item::findOrFail($id);
$item->doSomething();
return response()->json(true);
}
which is handled by an AJAX call:
$.ajax({
url: $(this).attr('href'),
type: 'GET',
dataType: 'json',
success: $.proxy(function(result) {
this.application.notification.showMessage('Job is being processed.');
}, this),
error: $.proxy(function(result) {
console.error(result);
}, this)
});
The notification system in Laravel allows you to send notifications to users over different channels.
Send Email Notifications in Laravel php use App\Notifications\Newvisit; Route::get('/', function () { $user = App\User::first(); $user->notify(new Newvisit("A new user has visited on your application.")); return view('welcome'); });
In order to use the redis queue driver, you should configure a Redis database connection in your config/database. php configuration file. This command will create a migration file in the database/migrations folder. The newly created file will contain the schema for the jobs table which we need to process the queues.
You can user Queue::after function on your AppServiceProvider
Import this dependencies
use Illuminate\Support\Facades\Queue;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
And on boot method you would use it
public function boot()
{
Queue::before(function (JobProcessing $event) {
// $event->connectionName
// $event->job
// $event->job->payload()
});
Queue::after(function (JobProcessed $event) {
// $event->connectionName
// $event->job
// $event->job->payload()
});
}
Probably I'm late for the party guys, but there are several options i can think of. When user clicks button from the front-end you can give attribute disabled to it and some text like 'processing'. Then you can:
I think Forge is doing websockets using pusher the endpoint to see if server is active when you are trying to deploy new code. I can clearly see the communication if you open Devtools->Resources->Sockets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With