Home > other >  Laravel, dispatchAfterResponse does not release frontend request
Laravel, dispatchAfterResponse does not release frontend request

Time:11-11

I am trying to implement async job in laravel, so I can send email (using 3rd party API), but let user go in the frontend so request doesn't wait for email to be sent. I am using Laravel 6.18.

so I've created generic job with php artisan make:job EmailJob

I've set sleep for 60 seconds as a test of long email send.

then in my controller

   EmailJob::dispatchAfterResponse();
   return response()->json($obj,200);

In chrome console, I can see there is 200 response, however request is still no resolved, and there is no data returned, so my ajax/axios request still waits for full response, eventually it times out (60 seconds is too long), and produces error in frontend.

So question is, how to execute job after full response is sent ?

CodePudding user response:

You have to change the queue driver and run queue:worker

The following 2 resources will help you

  1. https://laravel-news.com/laravel-jobs-and-queues-101
  2. https://laravel.com/docs/6.x/queues#connections-vs-queues
  • Related