Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to notify the user that a job has completed in Laravel?

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)
});
like image 404
szaman Avatar asked Feb 11 '16 10:02

szaman


People also ask

What is notify () in Laravel?

The notification system in Laravel allows you to send notifications to users over different channels.

How do I get email notifications in Laravel?

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'); });

How do I queue notifications in Laravel?

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.


2 Answers

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()
        });
    }
like image 135
Leandro Castro Avatar answered Sep 30 '22 19:09

Leandro Castro


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:

  1. Just ping your endpoint to check if what job is performing is finished
  2. Use websockets

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.

like image 41
linkmarine Avatar answered Sep 30 '22 17:09

linkmarine