{"id":18929442,"url":"https://github.com/thecodingmachine/oo-amqp-client","last_synced_at":"2025-10-12T07:42:31.386Z","repository":{"id":57020163,"uuid":"62793196","full_name":"thecodingmachine/oo-amqp-client","owner":"thecodingmachine","description":"An object oriented wrapper on top of the php-amqplib","archived":false,"fork":false,"pushed_at":"2018-10-30T13:56:54.000Z","size":68,"stargazers_count":1,"open_issues_count":1,"forks_count":7,"subscribers_count":6,"default_branch":"1.1","last_synced_at":"2025-10-11T20:39:12.183Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-07T09:19:10.000Z","updated_at":"2024-12-28T21:51:57.000Z","dependencies_parsed_at":"2022-08-23T12:20:30.957Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/oo-amqp-client","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/thecodingmachine/oo-amqp-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Foo-amqp-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Foo-amqp-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Foo-amqp-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Foo-amqp-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/oo-amqp-client/tar.gz/refs/heads/1.1","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Foo-amqp-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279010671,"owners_count":26084785,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:32:48.288Z","updated_at":"2025-10-12T07:42:31.333Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Latest Stable Version](https://poser.pugx.org/mouf/oo-amqp-client/v/stable)](https://packagist.org/packages/mouf/oo-amqp-client)\n[![Total Downloads](https://poser.pugx.org/mouf/oo-amqp-client/downloads)](https://packagist.org/packages/mouf/oo-amqp-client)\n[![Latest Unstable Version](https://poser.pugx.org/mouf/oo-amqp-client/v/unstable)](https://packagist.org/packages/mouf/oo-amqp-client)\n[![License](https://poser.pugx.org/mouf/oo-amqp-client/license)](https://packagist.org/packages/mouf/oo-amqp-client)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thecodingmachine/oo-amqp-client/badges/quality-score.png?b=1.1)](https://scrutinizer-ci.com/g/thecodingmachine/oo-amqp-client/?branch=1.1)\n[![Build Status](https://travis-ci.org/thecodingmachine/oo-amqp-client.svg?branch=1.1)](https://travis-ci.org/thecodingmachine/oo-amqp-client)\n[![Coverage Status](https://coveralls.io/repos/thecodingmachine/oo-amqp-client/badge.svg?branch=1.1\u0026service=github)](https://coveralls.io/github/thecodingmachine/oo-amqp-client?branch=1.1)\n\nAbout Object Oriented AMQP Client\n=================================\n\nThis package contains an object oriented wrapper on top of php-amqplib helping work with RabbitMQ in a more object oriented way.\n\nUsing this package, *exchanges*, *bindings* and *queues* are represented as objects.\nThis is useful, especially if you want to inject those objects in your dependency injection container.\n\nInstallation\n============\n\n```\ncomposer require mouf/oo-amqp-client\n```\n\nUsage\n=====\n\nBefore using this library, you should be accustomed to the AMQP concepts. If you are not, we strongly advise you to start reading the [\"AMQP 0-9-1 Model Explained\" document from the RabbitMQ documentation](https://www.rabbitmq.com/tutorials/amqp-concepts.html).\n\nDone? Let's get started.\n\nCreating a client\n-----------------\n\nThe first thing you want to create is a `Client` object. A `Client` represents a connection to RabbitMQ (for those of you used to php-amqplib, it is both a connection AND a channel).\n\n```php\nuse Mouf\\AmqpClient\\Client;\n\n$client = new Client(\n    $rabbitmq_host,\n    $rabbitmq_port,\n    $rabbitmq_user,\n    $rabbitmq_password,\n    $rabbitmq_vhost = '/',\n    $rabbitmq_insist = false,\n    $rabbitmq_login_method = 'AMQPLAIN',\n    $rabbitmq_login_response = null,\n    $rabbitmq_locale = 'en_US',\n    $rabbitmq_connection_timeout = 3.0,\n    $rabbitmq_read_write_timeout = 3.0,\n    $rabbitmq_context = null,\n    $rabbitmq_keepalive = false,\n    $rabbitmq_heartbeat = 0\n);\n```\n\nNote: the `Client` class exposes a number of useful configuration methods (you do not need to use those if you don't know what they do):\n\n```php\npublic function setPrefetchSize($prefetchSize);\npublic function setPrefetchCount($prefetchCount);\npublic function setAGlobal($aGlobal);\n```\n\nCreating an exchange\n--------------------\n\nIn AMQP, *exchanges* are the objects that receive messages and are in charge of forwarding those messages to queues.\nYou must therefore define an `Exchange` objects to send messages.\n\n```php\nuse Mouf\\AmqpClient\\Objects\\Exchange;\n\n$exchange = new Exchange($client, 'exchange_name', 'fanout');\n```\n\nWhen creating an exchange, you pass to the constructor the `Client` object, the exchange name, and the exchange type.\n\nNote: the exchange will *self-register* in the client.\n\nYou can apply advanced configuration using configuration methods:\n\n```php\npublic function setPassive($passive);\npublic function setDurable($durable);\npublic function setAutoDelete($autoDelete);\npublic function setInternal($internal);\npublic function setNowait($nowait);\npublic function setArguments($arguments);\npublic function setTicket($ticket);\n```\n\nCreating a queue and a binding\n------------------------------\n\nMessages arriving to an exchange are forwarded to a *queue* through a *binding*.\n\nWe will now create a queue to store our messages.\n\n```php\nuse Mouf\\AmqpClient\\Objects\\Queue;\n\n$queue = new Queue($client, 'queue_name', [\n    new Consumer(function(AMQPMessage $msg) {\n        // Do some stuff with the received message\n    })\n]);\n```\n\nWhen creating a client, you pass to the constructor the `Client` object, the client name, and an array of `Consumer` objects (actually an array of objects implementing the `ConsumerInterface`).\n\nA `Consumer` object is an object that contains code that will be called each time a message is received.\n\nNote: the queue will *self-register* in the client.\n\nYou can apply advanced configuration to your queue using those configuration methods:\n\n```php\npublic function setPassive($passive);\npublic function setDurable($durable);\npublic function setExclusive($exclusive);\npublic function setAutoDelete($autoDelete);\npublic function setNoWait($noWait);\npublic function setArguments($arguments);\npublic function setTicket($ticket);\npublic function setDeadLetterExchange(Exchange $exchange);\npublic function setConfirm($confirm);\npublic function setConsumerCancelNotify(Queue $consumerCancelNotify);\npublic function setAlternateExchange(Queue $alternateExchange);\npublic function setTtl($ttl);\npublic function setMaxLength($maxLength);\npublic function setMaxPriority($maxPriority);\n```\n\nYou will certainly want to use the `setDurable` method if you want your queue to store messages in case of outage of the receiver.\n\nAt this point, we have an *exchange*, we have a *queue*, but both are not linked together. We need to **bind** those, using a `Binding` object.\n\n```php\nuse Mouf\\AmqpClient\\Objects\\Binding;\n\n$binding = new Binding($exchange, $queue);\n$client-\u003eregister($binding);\n```\n\nA `Binding` links an exchange to a queue.\n\n**Important**: unlike the `Exchange` and the `Queue`, a `Binding` does not self-register in the client. You have to declare it in the client yourself, using the `Client::register` method.\n\nDone? Let's send and receive messages!\n\nSending a message\n-----------------\n\nIn order to send a message, you simply use the `Exchange::publish` method:\n\n```php\n$exchange-\u003epublish(new Message('your message body'), 'message_key');\n// ... and that's it!\n```\n\nYou may still want to configure a bit more the sending of your message. The `Exchange::publish` method accepts a number of optional arguments:\n\n```php\npublic function publish(Message $message,\n                        string $routingKey,\n                        bool $mandatory = false,\n                        bool $immediate = false,\n                        $ticket = null);\n```\n\nAlso, the `Message` class can be tweaked with one of those methods:\n\n```php\npublic function setContentType(string $content_type);\npublic function setContentEncoding(string $content_encoding);\npublic function setApplicationHeaders(array $application_headers);\npublic function setDeliveryMode(int $delivery_mode);\npublic function setPriority(int $priority);\npublic function setCorrelationId(string $correlation_id);\npublic function setReplyTo(string $reply_to);\npublic function setExpiration(string $expiration);\npublic function setMessageId(string $message_id);\npublic function setTimestamp(\\DateTimeInterface $timestamp);\npublic function setType(string $type);\npublic function setUserId(string $user_id);\npublic function setAppId(string $app_id);\npublic function setClusterId(string $cluster_id);\n```\n\nReceiving messages\n------------------\n\nAs we already saw, the first step to receiving message is creating a queue and adding `Consumer` objects to that queue.\n\nWe still need to tell PHP to start listening, otherwise, the callbacks in the `Consumer` will never be called.\n\nThis can be done using the `ConsumerService` class.\n\n```php\n$consumerService = new ConsumerService($client, [\n    $queue\n]);\n\n$consumerService-\u003erun();\n```\n\nThe `ConsumerService` constructor takes the client in parameter, and the array of queues that must be listened to.\n\nThe `ConsumerService::run` method will start listening on arriving messages, in an infinite loop.\n\nNotice that you can use `$consumerService-\u003erun(true);` if you want to listen to one message only and return afterward.\n\nAcknowledgements and error handling\n-----------------------------------\n\nWhen you receive a message, an acknowledgement will not be sent before the `Consumer` has finished consuming the message.\n\nIf an exception is triggered in the `Consumer`, a `nack` will be sent instead to RabbitMQ.\n\nNote: if your consumer callback throws an exception implementing the `RetryableExceptionInterface` interface, the `nack` message will be sent with the \"requeue\" flag. The message will be requeued.\n\nNote: if your consumer callback throws an exception implementing the `FatalExceptionInterface` interface, the exception will be propagated by the consumer (hence leading to the crash of the consumer script). Otherwise, consumer will continue processing messages.\n\nExceptions are logged by default using the error_log function. You can override this behaviour by passing a PSR-3 compliant logger to the `AbstractConsumer` constructor.\n\n\nWriting your consumer as a class\n--------------------------------\n\nSo far, to create a consumer, we used the `Consumer` class that takes a callback as first constructor parameter.\n\nAs an alternative, you can extend the `AbstractConsumer` class and implement the `onMessageReceived` method:\n\n```php\nclass MyConsumer extends AbstractConsumer\n{\n    public function onMessageReceived($msg)\n    {\n        // Do some stuff.\n    }\n}\n```\n\n\n\nSending a message to a given queue\n----------------------------------\n\nIf you want to target a special queue and send a message to it directly, you have 2 options.\n\n**Option 1**: create a `DefaultExchange` object and pass the queue name as the key of the message.\n\n```php\nuse Mouf\\AmqpClient\\Objects\\DefaultExchange;\n\n$exchange = new DefaultExchange($client);\n// Simply pass the queue name as the second parameter of \"publish\".\n// Note: you do not need to bind the queue to the exchange. RabbitMQ does this automatically.\n$exchange-\u003epublish(new Message('your message body'), 'name_of_the_target_queue');\n// ... and that's it!\n```\n\n**Option 2**: use the `publish` method of the `Queue` object:\n\n```php\nuse Mouf\\AmqpClient\\Objects\\Queue;\n\n$queue = new Queue($client, 'queue_name', [\n    new Consumer(function(AMQPMessage $msg) {\n        // Do some stuff with the received message\n    })\n]);\n\n// Shazam! We are directly sending a message to the queue. No exchange needed!\n$queue-\u003epublish(new Message('your message body'));\n```\n\nNote: these are RabbitMQ specific features and might not work with other AMQP buses.\n\n\nSymfony console integration\n---------------------------\n\nThis package comes with 2 Symfony commands that you can use to send and receive messages.\n\n- `Mouf\\AmqpClient\\Commands\\PublishCommand` (`amqp:publish`) allows you to send an arbitrary message on an exchange (read from a file or from STDIN)\n- `Mouf\\AmqpClient\\Commands\\ConsumeCommand` (`amqp:consume`) listen to all configured queues\n\n\nRunning the unit tests\n======================\n\nThis package uses PHPUnit for unit tests.\n\nTo run the tests:\n\n```\nvendor/bin/phpunit\n```\n\nObviously, you need a running RabbitMQ server to test this package. If you use Docker, you can start one using:\n\n```sh\ndocker run -p 5672:5672 -p 15672:15672 rabbitmq:management\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Foo-amqp-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Foo-amqp-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Foo-amqp-client/lists"}