SimBioT
SimBioT

Reputation: 79

How to get all messages from the queue in php-amqplib (RabbitMQ)?

Currently, I'm using https://github.com/php-amqplib/php-amqplib and I've read a lot of examples in this repository but I still don't understand how to get all the messages from the queue?

I just need to receive some messages, group them by value and perform an action.

Is it possible to do with RabbitMQ at all?

How can I implement this in php?

Upvotes: 0

Views: 5387

Answers (2)

Christian Bergau
Christian Bergau

Reputation: 646

<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;

$connection = new AMQPStreamConnection('rabbitmq', 5672, 'guest', 'guest');
$channel = $connection->channel();
$queueName = 'task_queue';
$channel->queue_declare($queueName, false, true, false, false);
$result = $channel->basic_get($queueName);

var_dump($result);

$channel->close();
$connection->close();

Upvotes: 6

Nomad
Nomad

Reputation: 206

You cannot get all messages currently available by calling single method.
Closest possible solution is by using basic_consume method.

For example:

function process_message($message)
{
    echo "Received message '" . $message->body . "'\n";

    /** Do your grouping here **/

}

$channel->basic_consume($queue, '', false, false, false, false, 'process_message');

// Loop as long as the channel has callbacks registered
while (count($channel->callbacks)) {
    $channel->wait();
}

You can check official RabbitMQ PHP tutorial or demo from php-amqplib.

Upvotes: 0

Related Questions