Jesse Weigert Jesse Weigert - 4 months ago 53
PHP Question

How do I use the RabbitMQ delayed message queue from PHP?

I'm trying to use the Delayed Message Queue for RabbitMQ from PHP, but my messages are simply disappearing.

I'm declaring the exchange with the following code:

false, /* passive, create if exchange doesn't exist */
true, /* durable, persist through server reboots */
false, /* autodelete */
false, /* internal */
false, /* nowait */
['x-delayed-type' => ['S', 'direct']]);

I'm binding the queue with this code:

false, /* Passive */
true, /* Durable */
false, /* Exclusive */
false /* Auto Delete */
$this->channel->queue_bind($queueName, "delay", $queueName);

And I'm publishing a message with this code:

$msg = new AMQPMessage(json_encode($msgData), [
'delivery_mode' => 2,
'x-delay' => 5000]);
$this->channel->basic_publish($msg, 'delay', $queueName);

But the message doesn't get delayed; it's still immediately delivered. What am I missing?


you need a routing key to publish from the exchange to the queue in question.

the reason publishing to the built-in direct exchange works, is because this exchange is a special case that uses the routing key as the destination queue name.

for all exchanges and queues that you create, you need to creating a binding between the exchange and the queue, with a routing key. then you publish the message with that routing key instead of the destination queue name.

i don't know the PHP code to create the binding... but it generally looks something like this:

channel.bind(exhange_name, queue_name, routing_key)

then in your publishing of the message:

$this->channel->basic_publish($msg, 'delay', $routing_key);