BunnyPHP Documentation

repository·0.6.x·Indexed 20 days ago

https://github.com/jakubkulhan/bunny

A performant, pure-PHP AMQP (RabbitMQ) library implementing the AMQP 0.9.1 protocol. Designed for non-blocking applications using ReactPHP, it utilizes PHP Fibers to provide an asynchronous client with a synchronous-feeling API. Requires PHP 8.1 or newer.

Tokens
6.6K
Snippets
29
Records
31
Agent score
73%

What's inside BunnyPHP

  1. How Fibers work in BunnyPHP

    0.6.x

    Since version 0.6, BunnyPHP is built using PHP Fibers. This means that every method on Client and Channel must be called inside a fiber.

    In a ReactPHP environment, this is typically achieved by wrapping the call in Loop::futureTick(async(static function() { ... })). In other applications, any trigger that ensures the direct call stack is inside a fiber will satisfy this requirement.

    use Bunny\Client;
    use Bunny\Configuration;
    use React\EventLoop\Loop;
    
    use function React\Async\async;
    
    $configuration = new Configuration(
        host:     'HOSTNAME',
        vhost:    'VHOST',
        user:     'USERNAME',
        password: 'PASSWORD',
    );
    
    $bunny = new Client($configuration);
    Loop::futureTick(async(static function (): void {
      $bunny->connect();
    }));
  2. Connect to RabbitMQ securely using TLS/SSL via DSN

    0.6.x

    TLS options can be passed within a DSN string using query parameters in the format tls[key]=value.

    use Bunny\Client;
    use Bunny\Configuration;
    
    $configuration = Configuration::fromDSN(
        'amqp://USERNAME:PASSWORD@HOSTNAME/VHOST?tls[cafile]=ca.pem&tls[local_cert]=client.cert&tls[local_pk]=client.key',
    );
    
    $bunny = new Client($configuration);
    $bunny->connect();
  3. Subscribe to a queue (Continuous Consumption)

    0.6.x

    Use the consume method to subscribe to a queue. This runs indefinitely. Inside the callback, you should acknowledge (ack) successful messages or negatively acknowledge (nack) failed ones to trigger redelivery.

    $channel->consume(
        static function (Message $message, Channel $channel, Client $bunny) {
            $success = handleMessage($message);
    
            if ($success) {
                $channel->ack($message);
                return;
            }
    
            $channel->nack($message);
        },
        'queue_name',
    );
  4. Pop a single message from a queue

    0.6.x

    To retrieve a single message from a queue without continuous subscription, use the get method and manually acknowledge the message.

    $message = $channel->get('queue_name');
    
    // Handle message
    
    $channel->ack($message);
  5. Connect to RabbitMQ using a DSN

    0.6.x

    You can initialize a Bunny\Configuration using an AMQP DSN string via the fromDSN method.

    use Bunny\Client;
    use Bunny\Configuration;
    
    $configuration = Configuration::fromDSN('amqp://USERNAME:PASSWORD@HOSTNAME/VHOST');
    
    $bunny = new Client($configuration);
    $bunny->connect();
  6. Publish a message to a queue

    0.6.x

    To publish a message, you must first obtain a channel and declare the target queue.

    Note on Quorum Queues: If your virtual host is configured to use Quorum queues by default (standard in RabbitMQ 4), use queueDeclare with the durability parameter set to true: $channel->queueDeclare('queue_name', false, true);

    Messages can be published using positional arguments or named arguments.

    // Using positional arguments
    $channel->publish(
        $message,    // string
        [],          // headers
        '',          // exchange
        'queue_name', // routing key
    );
    
    // Using named arguments (recommended)
    $channel->publish(
        body:       $message,
        routingKey: 'queue_name',
    );
  7. Regenerate Bunny protocol code from specification

    0.6.x

    Most code in the Bunny\Protocol namespace is automatically generated from the AMQP specification file located at spec/amqp-rabbitmq-0.9.1.json.

    Warning: Do not edit these files directly; they contain DO NOT EDIT! comments. To make changes to the protocol implementation, modify spec/generate.php and run the generator script.

    $ php ./spec/generate.php
  8. Connect to RabbitMQ securely using TLS/SSL

    0.6.x

    To use TLS/SSL, provide a tls array in the Configuration constructor. This array accepts standard PHP SSL context options.

    Common keys include:

    • cafile: Path to the CA certificate.
    • local_cert: Path to the client certificate.
    • local_pk: Path to the client private key.

    Invalid TLS configurations will cause connection failure.

    use Bunny\Client;
    use Bunny\Configuration;
    
    $configuration = new Configuration(
        host:     'HOSTNAME',
        vhost:    'VHOST',
        user:     'USERNAME',
        password: 'PASSWORD',
        tls:      [
            'cafile'      => 'ca.pem',
            'local_cert'  => 'client.cert',
            'local_pk'    => 'client.key',
        ],
    );
    
    $bunny = new Client($configuration);
    $bunny->connect();