0

I'm working on a Laravel 8 project, and have a Laravel Lumen 8 project sitting along side my Laravel project. Both connect to the same database, and the database has the jobs and failed_jobs table.

Ideally, I want to simply dispatch an Event from my Laravel project and have my smaller, microservice Laravel Lumen project listen for this event in some way and process it and save something back to my database.

I've generated an Event called RedirectWatcher in my Laravel project and need a LogRedirect listener in my Lumen project to process this and save an entry to the DB.

My issue is that I have to register both the event and listener in both projects, like:

protected $listen = [
    'App\Events\ExampleEvent' => [
        'App\Listeners\ExampleListener',
    ],
];

And the event wouldn't exist in the Lumen project.

What's the best way to handle this without duping code?

Ryan H
  • 2,620
  • 4
  • 37
  • 109
  • Does the lumen application has any other purpose apart from processing the Jobs dispatched by the Laravel application? – harish durga Feb 25 '22 at 12:45
  • Nope, it's purely a processing of data from the Laravel application. – Ryan H Feb 25 '22 at 13:06
  • Then why don't you run another Laravel instance with the same code configured to listen from the queue instead of running lumen? – harish durga Feb 25 '22 at 13:48
  • One more option is to maintain the common classes between the projects as a separate laravel package and install that via the composer. – harish durga Feb 25 '22 at 14:57

1 Answers1

0

An event class is simply a data container which holds the information related to the event, and the listener is where you handle business logic.

So I think best approach is to register en Event without listener in Laravel application and create a job (not listener) in Lumen application.So LogRedirect will be a job not a listener.

Make sure your event implements Illuminate\Contracts\Broadcasting\ShouldBroadcast interface.

  • Then this Job class has to be there in both Laravel and lumen which again gives rise to code duplication. – harish durga Feb 25 '22 at 13:49
  • Why you need to have a job class in laravel as well? You are saving job from one application and executing it from another, am I right? – Sona Khachatryan Feb 25 '22 at 13:51
  • Otherwise, how can the Job be dispatched to the Queue? – harish durga Feb 25 '22 at 13:52
  • You can create and dispatch en events even without listeners. It is not required to have listener for event. Just create event without listener. – Sona Khachatryan Feb 25 '22 at 13:54
  • Make sure you event implements `Illuminate\Contracts\Broadcasting\ShouldBroadcast` interface. – Sona Khachatryan Feb 25 '22 at 14:11
  • Yes, we don't listener class to an event. So when an event is fired how will be queued/sent to the queue. In the documentation, it is said only the listeners will be queued not the events. So when an event is fired, the associated Listener class implementing `ShouldQueue` will be sent to queue instead of getting processed in sync. – harish durga Feb 25 '22 at 14:14
  • 1
    As I mentioned above make sure you event implements `Illuminate\Contracts\Broadcasting\ShouldBroadcast` interface. – Sona Khachatryan Feb 25 '22 at 14:18
  • `ShouldBroadcast` is not for pushing events to queues but to send notifications to channels. When the notification implements `ShouldBroadcast` contact, there should be `broadcastOn` method which should return a `Channel`. – harish durga Feb 25 '22 at 14:32