Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consume raw json queue messages in Laravel

Tags:

php

laravel

Normally Laravel expects that it queued up any messages that it later consumes. It creates a payload with a job attribute that later indicates how to handle the queue message. When you do queue up jobs with Laravel, and later process them with Laravel, it works great!

However, I have some non-Laravel apps that are posting json messages to a queue. I need Laravel to pick up these messages and handle them.

I can write a command bus job to handle the messages, but I haven't been able to figure out how to tell queue:work to send the messages on to my specific handler.

It seems Laravel has a hard assumption that any queue messages it is asked to handle will be properly formatted, serialized, and structured the way it expects them to be.

How can I have Laravel pick up these raw json payloads, ignore the structure (there's nothing there for it to understand), and simply hand the payload off to my handler?

For example, if I have a queue message similar to:

{
    "foo" : "bar"
}

So again, there's nothing for Laravel to inspect or understand here.

But I have a job handler that knows how to handle this:

namespace App\Jobs;

class MyQueueHandler {
    public function handle($payload) {
        Log::info($payload['foo']); // yay!
    }
}

Now how to get queue:work and queue:listen to simply hand off any payloads to this App\Jobs\MyQueueHandler handler, where I can do the rest on my own?

like image 799
jszobody Avatar asked Sep 10 '15 15:09

jszobody


1 Answers

If you're using Laravel 5.6+, check out this package.

like image 76
brice Avatar answered Oct 06 '22 15:10

brice