PHP code example of phpdot / rabbitmq

1. Go to this page and download the library: Download phpdot/rabbitmq library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpdot / rabbitmq example snippets


use PHPdot\RabbitMQ\RabbitMQConnection;
use PHPdot\RabbitMQ\Config\RabbitMQConfig;
use PHPdot\RabbitMQ\Message;
use PHPdot\RabbitMQ\Enum\TaskStatus;

$conn = new RabbitMQConnection(new RabbitMQConfig(
    host: 'localhost',
    exchanges: ['tasks' => ['type' => 'direct', 'durable' => true]],
    queues: [
        'tasks.process' => [
            'bindings' => [['exchange' => 'tasks', 'routing_key' => 'task.new']],
            'durable' => true,
        ],
    ],
));

$conn->message('{"task":"send_email"}')->publish('tasks', 'task.new');

$conn->consume('tasks.process')->execute(function (Message $msg): TaskStatus {
    processTask(json_decode($msg->body(), true));

    return TaskStatus::SUCCESS;
});

$result = $conn->replay('tasks.process.dead')
    ->limit(10)
    ->execute(); // or ->dryRun() to preview
mermaid
graph TD
    APP["Application / phpdot/pool"]
    CONNECTOR["RabbitMQConnector<br/><br/>Contracts Pool ConnectorInterface"]
    CONN["RabbitMQConnection<br/><br/>topology, resilient reconnect,<br/>channel lifecycle"]
    OPS["Publisher / Consumer<br/>TopologyManager / Replayer<br/><br/>publish, consume, retry + DLQ, replay"]
    CLI["Cli\\Command\\*<br/><br/>Symfony Console: status, queues,<br/>peek, replay, dlq:analyze"]
    AMQP["php-amqplib<br/><br/>AMQPStreamConnection + channel"]

    APP --> CONNECTOR
    CONNECTOR --> CONN
    APP --> OPS
    CLI --> OPS
    OPS --> CONN
    CONN --> AMQP