Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute events asynchronously in laravel

Pls I'm still new to laravel and I have used events in laravel a couple of times but I'm curious and would like to know if it's possible to execute an event in laravel asynchronously. Like for instance in the code below:

    <?php

    namespace mazee\Http\Controllers;

   class maincontroller extends Controller
   {
    public  function index(){
       Event::fire(new newaccountcreated($user)) ;
      //do something

    }

Is it possible for the block of code in the event listener of the "newaccountcreated" event to be executed asynchronously after the event is fired ?

like image 260
Ewomazino Ukah Avatar asked Sep 29 '16 18:09

Ewomazino Ukah


1 Answers

Yes of course this is possible. You should read about Laravel Queues. Every driver (only not sync driver) are async. The easiest to configure is the database driver, but you can also want to try RabbitMQ server , here is Laravel bundle for it.

You can also add to your EventListener: newaccountcreated trait Illuminate\Queue\InteractsWithQueue (you can read about him here) which will helps you to connect it with Laravel Queue.

like image 83
Filip Koblański Avatar answered Sep 28 '22 11:09

Filip Koblański