Search by

videlalvaro / php-amqplib

videlalvaropostalservice14nubeiro

This package is abandoned and no longer maintained. The author suggests using the php-amqplib/php-amqplib package instead.

Formerly videlalvaro/php-amqplib. This library is a pure PHP implementation of the AMQP protocol. It's been tested against RabbitMQ.

Package info

github.com/php-amqplib/php-amqplib

Homepage

pkg:composer/videlalvaro/php-amqplib

Statistics

Installs: 7 003 512

Dependents: 92

Suggesters: 14

Stars: 4 599

Open Issues: 21

v3.8.0 2026-12-02 20:54 UTC

README

PHPUnit tests Latest Version on Packagist Total Downloads Software License codecov

php-amqplib is a pure PHP implementation of the AMQP 0-9-1 protocol. It is primarily tested against RabbitMQ.

This library was originally used for the PHP examples in RabbitMQ in Action and the official RabbitMQ tutorials.

Table of Contents

Features

  • Pure PHP: No C extensions required.
  • AMQP 0-9-1 Support: Compatible with RabbitMQ 3.x and later.
  • RabbitMQ Extensions:
    • Exchange to Exchange Bindings
    • Basic Nack
    • Publisher Confirms
    • Consumer Cancel Notify
  • Connection Pooling: Compatible with AMQProxy for lower connection churn.
  • Interop: Compatible with enqueue/amqp-lib for AMQP interop.

Project Status & Maintainers

The project is currently maintained by Ramūnas Dronga, Luke Bakken, and several VMware engineers.

We welcome contributions! Please see CONTRIBUTING.md for details.

Note: This project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Requirements

Installation

Ensure you have Composer installed, then run:

composer require php-amqplib/php-amqplib

This fetches the library into your vendor folder. You can then include the autoloader in your PHP files:

require_once __DIR__.'/vendor/autoload.php';

Import the necessary classes:

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

Quick Start

To try the library, open two terminals and start a consumer and publisher.

Terminal 1 (Consumer):

cd demo
php amqp_consumer.php

Terminal 2 (Publisher):

cd demo
php amqp_publisher.php "some text to publish"

You should see the message arrive in the first terminal. To stop the consumer, send a quit message:

php amqp_publisher.php quit

For a non-blocking consumer example, see demo/amqp_consumer_non_blocking.php.

Advanced Usage

Batch Publishing

If you are publishing many messages to the same exchange/routing key, you can use batch_basic_publish to improve performance.

$ch = $connection->channel();

// Queue messages
$msg = new AMQPMessage('body 1');
$ch->batch_basic_publish($msg, $exchange);

$msg2 = new AMQPMessage('body 2');
$ch->batch_basic_publish($msg2, $exchange);

// Send all queued messages at once
$ch->publish_batch();

You can decide when to flush the batch (e.g., every 50 messages or 100) based on your throughput requirements.

Optimized Message Publishing

If you are publishing many messages with identical properties (e.g., content type, delivery mode), reuse the AMQPMessage instance and update only the body. This avoids re-encoding the properties header for every message.

$properties = [
    'content_type' => 'text/plain',
    'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
];

// Create the message once
$msg = new AMQPMessage('', $properties);

// Loop and update only the body
foreach ($data as $body) {
    $msg->setBody($body);
    $ch->basic_publish($msg, $exchange);
}

Truncating Large Messages

AMQP does not limit message size, but PHP's memory_limit might be hit if a very large message is received before the consumer callback is called.

You can set a body size limit on the channel:

$channel->setBodySizeLimit(1024 * 1024); // 1MB limit
  • Messages exceeding this limit are truncated.
  • The message object will have $is_truncated set to true and $body_size reflecting the original size.
  • To disable truncation, call setBodySizeLimit(0).

Connection Recovery

Since php-amqplib is single-threaded, you should implement connection recovery in your application logic using exception handling.

Common connection exceptions include:

  • PhpAmqpLib\Exception\AMQPConnectionClosedException
  • PhpAmqpLib\Exception\AMQPIOException
  • \RuntimeException
  • \ErrorException

Example Recovery Loop:

$connection = null;
$channel = null;

while (true) {
    try {
        if (!$connection) {
            $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
            $channel = $connection->channel();
        }
        
        // Subscribe to single queue
        $channel->basic_consume('my_queue', '', false, false, false, false, $callback);
        // Wait and process all incoming messages in an endless loop until connection exception or manual stop using self::stopConsume()
        $channel->consume();       
    } catch (\PhpAmqpLib\Exception\AMQPException $e) {
        // Log and clean up
        if ($channel) {
            $channel->close();
        }
        if ($connection) {
            $connection->close();
        }
        $connection = null;
        $channel = null;
        
        // Wait before retrying
        sleep(1);
        
    } catch (\Exception $e) {
        // Handle application errors differently
        // ...
    }
}

See demo/connection_recovery_consume.php for a full example.

Unix Signals

If the PCNTL extension is installed (common on Linux), signals are dispatched between consumer loops. You can register custom handlers:

$pcntlHandler = function ($signal) {
    switch ($signal) {
        case \SIGTERM:
        case \SIGINT:
            // Cleanup and exit
            exit(0);
        case \SIGHUP:
            // Restart logic
            break;
    }
};

pcntl_signal(\SIGTERM, $pcntlHandler);
pcntl_signal(\SIGINT,  $pcntlHandler);

To disable signal handling, define the constant before loading the library:

define('AMQP_WITHOUT_SIGNALS', true);

Signal-based Heartbeat

If using PCNTL (Linux), you can register a heartbeat sender to keep connections alive:

use PhpAmqpLib\Heartbeat\PCNTLHeartbeatSender;

$sender = new PCNTLHeartbeatSender($connection);
$sender->register();

// ... your code ...

$sender->unregister();

Debugging

To enable protocol-level debugging, define this constant before running your script:

define('AMQP_DEBUG', true);

API Documentation

http://php-amqplib.github.io/php-amqplib/

Tutorials

  • Official RabbitMQ Tutorials
  • amqp_ha_consumer.php: Mirrored queues
  • amqp_consumer_exclusive.php: Fanout exchanges with exclusive queues
  • amqp_consumer_fanout_{1,2}.php: Fanout exchanges with named queues
  • amqp_consumer_pcntl_heartbeat.php: Signal-based heartbeat usage
  • basic_get.php: Using basic_get to poll for messages

Contributing

Please see CONTRIBUTING.md for details on how to run tests and submit changes.

License

This project is licensed under the LGPL 2.1. See LICENSE for more information.