Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Laravel / RabbitMQ => How to listen and handle jobs emitted by external services ?

Hello,

I work on an admin application with Laravel / VueJS to manage microservices provided by several nodejs applications.

First step, I have tried to connect to RabbitMQ. For that I'm using the vladimir-yuldashev/laravel-queue-rabbitmq, I have tried to dispatch a job from the Laravel application and then handle it from the Laravel application, all works fine. To do that, I have binded the job to a function to execute when the job is handled.

Now I need to consume jobs sent to RabbitMQ by other applications than the Laravel one. That means that I don't have any job defined inside Laravel because the job has been created and sent to RabbitMQ by another application.

How can I listen to those messages and handle them ?

Thanks for your help.

V

0 likes
11 replies
LaryAI's avatar
Level 58

To listen and handle jobs emitted by external services using RabbitMQ in a Laravel application, you can follow these steps:

  1. Install the Package: Make sure you have the vladimir-yuldashev/laravel-queue-rabbitmq package installed and configured in your Laravel application.

  2. Configure RabbitMQ Connection: Ensure that your config/queue.php file has the RabbitMQ connection properly set up, including the correct queue names that the external services will be using.

  3. Create a Listener: Since the jobs are not dispatched by Laravel, you won't have a job class to handle them. Instead, you'll need to create a listener that can process the incoming data.

  4. Process the Message: In your listener, you will need to manually handle the message payload and perform the necessary actions.

Here's an example of how you might set up a listener to handle incoming messages from RabbitMQ:

use Illuminate\Support\Facades\Queue;
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;

// Assuming you have already set up the RabbitMQ connection in config/queue.php

Queue::connection('rabbitmq')->before(function ($event) {
    // You can perform some action before consuming the message
});

Queue::connection('rabbitmq')->after(function ($event) {
    // You can perform some action after consuming the message
});

Queue::connection('rabbitmq')->looping(function ($event) {
    // This is where you can consume the message
    $message = $event->message;

    // Decode the message if it's in JSON format or process it as needed
    $data = json_decode($message->body, true);

    // Now you can handle the data from the message
    // For example, you might have a switch or if-else block to handle different types of jobs
    switch ($data['type']) {
        case 'jobTypeA':
            // Handle job type A
            break;
        case 'jobTypeB':
            // Handle job type B
            break;
        // Add more cases as needed for different job types
    }

    // Acknowledge the message so it is removed from the queue
    $message->ack();
});

Queue::connection('rabbitmq')->failing(function ($event) {
    // Log the failed job or perform some other action
});

In this example, we're using the looping method to listen for incoming messages. When a message is received, we decode it and handle it according to its type. After processing the message, we acknowledge it to remove it from the queue.

Remember to replace 'rabbitmq' with the actual name of your RabbitMQ connection as defined in your config/queue.php.

Finally, you will need to run the Laravel queue worker to start listening to the RabbitMQ queue:

php artisan queue:work rabbitmq --queue=your_queue_name

Replace your_queue_name with the name of the queue you want to listen to.

This setup allows your Laravel application to consume and process messages sent to RabbitMQ by external services, even when those messages do not correspond to Laravel job classes.

vincent15000's avatar

@LaryAI This reply seems to be pretty interesting, but it doesn't work and I don't understand where RabbitMQConnector is used.

martinbean's avatar
Level 80

@vincent15000 Lary replied with a helpful answer for a change.

In this example, we're using the looping method to listen for incoming messages. When a message is received, we decode it and handle it according to its type. After processing the message, we acknowledge it to remove it from the queue.

This is what you need to do, and Lary created a code example for doing just this:

Queue::connection('rabbitmq')->looping(function ($event) {
    // This callback will be executed for each message in your RabbitMQ queue

    // Fetch and decode the message payload
    $message = $event->message;

    $data = json_decode($message->body, true);

    // Do whatever with the event based on the message contents here...

    // Acknowledge the message so it is removed from the queue
    $message->ack();
});

You can’t use queued job classes as, like you say yourself, you don’t have one if these are just raw messages pushed via other providers.

1 like
vincent15000's avatar

@martinbean Thank you ... yes I have tested this, but there is no looping function defined for the connection.

Perhaps due to the package I'm using ?

vladimir-yuldashev/laravel-queue-rabbitmq

vincent15000's avatar

@martinbean I have solved it by declaring a custom Job class.

class MyRabbitMQJob extends RabbitMQJob
{
    public function fire()
    {
        $message = $this->payload();
        Log::info($message);

        $payload = $this->payload();

        $class = MessageConsumerService::class;
        $method = 'consume';

        ($this->instance = $this->resolve($class))->{$method}($this, $payload);

        $this->delete();
    }
}

I will remember Lary's suggestion if I decide to remove the package and keep only the AMQP package.

But ... a question ... where do I have to put this code with the looping part ?

vincent15000's avatar

@martinbean Yes but what I mean is that I get an error message saying that there isn't any looping function.

vincent15000's avatar

@martinbean Yes I effectively already have done that.

But now I wonder if it's not easier to use only the base PHP package for AMQP.

Do you know if it's possible to get all the existing queues names ? I mean ... if I don't know the name of a queue, it's not possible to listen to the queue.

vincent15000's avatar

@martinbean Well ... I imagine situations where I have a topic exchange and inside this exchange, some external applications can create queues and the main backend application doesn't know the name of these queues, but has to listen to them.

Please or to participate in this conversation.