The PHP AMQPQueue::consume API performs a focused operation on an object or value. The examples below show its effect on actual data.
Consumes messages and invokes a callback for deliveries from the queue.
What Is AMQPQueue::consume in PHP?
Consumes messages and invokes a callback for deliveries from the queue.
This API requires a relevant extension or PHP version and cannot be reflected in the local PHP 8.4 runtime. Refer to the version or extension note below before using the examples.
Compatibility note: Install the PECL AMQP extension and configure a RabbitMQ-compatible broker before running this example.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
$connection = new AMQPConnection([
"host" => getenv("AMQP_HOST") ?: "127.0.0.1",
"port" => 5672,
"login" => getenv("AMQP_USERNAME") ?: "guest",
"password" => getenv("AMQP_PASSWORD") ?: "guest"
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName("events.exchange");
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declareExchange();
$queue = new AMQPQueue($channel);
$queue->setName("events.queue");
$queue->declareQueue();
$queue->bind($exchange->getName(), "events.created");
$exchange->publish("sample event", "events.created");
$queue->consume(function (AMQPEnvelope $message, AMQPQueue $activeQueue): bool {
echo $message->getBody();
$activeQueue->ack($message->getDeliveryTag());
return false;
});
$connection->disconnect();
Consumes messages and invokes a callback for deliveries from the queue.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
$connection = new AMQPConnection([
"host" => getenv("AMQP_HOST") ?: "127.0.0.1",
"port" => 5672,
"login" => getenv("AMQP_USERNAME") ?: "guest",
"password" => getenv("AMQP_PASSWORD") ?: "guest"
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName("audit.exchange");
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declareExchange();
$queue = new AMQPQueue($channel);
$queue->setName("audit.queue");
$queue->declareQueue();
$queue->bind($exchange->getName(), "events.created");
$exchange->publish("sample event", "events.created");
$queue->consume(function (AMQPEnvelope $message, AMQPQueue $activeQueue): bool {
echo $message->getBody();
$activeQueue->ack($message->getDeliveryTag());
return false;
});
$connection->disconnect();
Queue operations require a configured AMQP broker; do not acknowledge before processing succeeds, and do not purge a production queue casually.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
$connection = new AMQPConnection([
"host" => getenv("AMQP_HOST") ?: "127.0.0.1",
"port" => 5672,
"login" => getenv("AMQP_USERNAME") ?: "guest",
"password" => getenv("AMQP_PASSWORD") ?: "guest"
]);
$connection->connect();
$channel = new AMQPChannel($connection);
$exchange = new AMQPExchange($channel);
$exchange->setName("jobs.exchange");
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declareExchange();
$queue = new AMQPQueue($channel);
$queue->setName("jobs.queue");
$queue->declareQueue();
$queue->bind($exchange->getName(), "events.created");
$exchange->publish("sample event", "events.created");
$queue->consume(function (AMQPEnvelope $message, AMQPQueue $activeQueue): bool {
echo $message->getBody();
$activeQueue->ack($message->getDeliveryTag());
return false;
});
$connection->disconnect();
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Queue operations require a configured AMQP broker; do not acknowledge before processing succeeds, and do not purge a production queue casually.
- Consumes messages and invokes a callback for deliveries from the queue.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
AMQPQueue::consume | Consumes messages and invokes a callback for deliveries from the queue. |
| AMQPExchange | Queues hold deliveries and handle acknowledgments; exchanges distribute publications to queues or other exchanges. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of AMQPQueue::consume | Queue operations require a configured AMQP broker; do not acknowledge before processing succeeds, and do not purge a production queue casually. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Queues hold deliveries and handle acknowledgments; exchanges distribute publications to queues or other exchanges. |
Key Takeaways
- Consumes messages and invokes a callback for deliveries from the queue.
- Queue operations require a configured AMQP broker; do not acknowledge before processing succeeds, and do not purge a production queue casually.
- Use the example that matches the values, runtime version and dependencies in your application.
AMQPQueue::consume is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.