PHPackages                             marwanalsoltany/amqp-agent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. marwanalsoltany/amqp-agent

ActiveLibrary[Queues &amp; Workers](/categories/queues)

marwanalsoltany/amqp-agent
==========================

An elegant wrapper around the famous php-amqplib for 90% use case.

v2.2.1(3y ago)161.8kLGPL-2.1-or-laterPHPPHP &gt;=7.1CI failing

Since Jun 15Pushed 3y ago2 watchersCompare

[ Source](https://github.com/MarwanAlsoltany/amqp-agent)[ Packagist](https://packagist.org/packages/marwanalsoltany/amqp-agent)[ Docs](https://github.com/MarwanAlsoltany/amqp-agent#readme)[ Fund](https://ko-fi.com/marwanalsoltany)[ RSS](/packages/marwanalsoltany-amqp-agent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (14)Used By (0)

[AMQP Agent](https://marwanalsoltany.github.io/amqp-agent/ "Documentation")
===========================================================================

[](#amqp-agent)

An elegant wrapper around the famous php-amqplib for 90% use case.

[![PHP Version](https://camo.githubusercontent.com/7701df78d2a513cc7c8b71612c4ab59e16b42f69b1ec56619c1a6526c36af2d5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545372e312d79656c6c6f773f7374796c653d666c61742d737175617265)](https://github.com/MarwanAlsoltany/amqp-agent/search?l=php)[![Latest Version on Packagist](https://camo.githubusercontent.com/f4e29f1c8e918e70390b3a06e52cb9787dbf5aac72b97be1e24a1e172c1fe502/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d617277616e616c736f6c74616e792f616d71702d6167656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/marwanalsoltany/amqp-agent)[![License](https://camo.githubusercontent.com/2cfe57367d883f35536e7bc1322e6066674f659f2f0a6ef4d0a2df1faddb6e08/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4c47504c5f322e315f6f725f6c617465722d7265642e7376673f7374796c653d666c61742d737175617265)](./LICENSE)[![Maintenance](https://camo.githubusercontent.com/14b6e74620b6bb1cf9e83e5fa31ffe779925694d8c1b6d701759c8cd71fbce44/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65642d7965732d6f72616e67652e7376673f7374796c653d666c61742d737175617265)](https://github.com/MarwanAlsoltany/amqp-agent/graphs/commit-activity)[![Documentation](https://camo.githubusercontent.com/e2cf1ba1ee0332b5eb0445784ff51a78020c0ea94569b68e76fd08c6eb8e227b/68747470733a2f2f696d672e736869656c64732e696f2f776562736974652d75702d646f776e2d626c75652d7265642f687474702f6d617277616e616c736f6c74616e792e6769746875622e696f2f616d71702d6167656e742e7376673f7374796c653d666c61742d737175617265)](http://marwanalsoltany.github.io/amqp-agent)[![Total Downloads](https://camo.githubusercontent.com/689bff6994a3e0309b4d90c76bcf7d7ead06675a659ecf19883116ce994b5905/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d617277616e616c736f6c74616e792f616d71702d6167656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/marwanalsoltany/amqp-agent/stats)[![Scrutinizer Build Status](https://camo.githubusercontent.com/e60ebcb75e187715b46caf78d3b1ffb33858bc28b079ce0bfbeacc66028d3c35/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f6275696c642f672f4d617277616e416c736f6c74616e792f616d71702d6167656e742f6d61737465723f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/MarwanAlsoltany/amqp-agent/build-status/master)[![Scrutinizer Code Coverage](https://camo.githubusercontent.com/1f6b4d4ea57912b18d9d5db114a03e36f50fc82203c710754558a7a4fa348782/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f4d617277616e416c736f6c74616e792f616d71702d6167656e742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/MarwanAlsoltany/amqp-agent/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c8dbbd82bf38c83f6f49ef9623aa807f9f6e0ee9aaf552d1f779aa0fef94e0f2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f4d617277616e416c736f6c74616e792f616d71702d6167656e742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/MarwanAlsoltany/amqp-agent/?branch=maste)[![Travis Build Status](https://camo.githubusercontent.com/edc015d639a6a5ca82d9686d169ca654760ca420492b2d0ca7b5d5165b80b5a0/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4d617277616e416c736f6c74616e792f616d71702d6167656e742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.com/MarwanAlsoltany/amqp-agent)[![StyleCI Code Style](https://camo.githubusercontent.com/5f9fd6b9325158e91d9e88c0d4bb65671a5550b3d481843e6f2a2c8dc59d1bae/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3237313934343936322f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/271944962)

Table of Contents[Installation](#installation)
[About AMQP Agent](#about-amqp-agent)
[API](#api)
[Documentation](https://marwanalsoltany.github.io/amqp-agent/)
[Configuration](#configuration)
[Examples](#examples)
[Links](#links)
[License](#license)
[Changelog](./CHANGELOG.md)

[![Tweet](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?url=&text=Working%20with%20%23RabbitMQ%20in%20%23PHP%20has%20never%20been%20so%20easy%20and%20fun%2C%20check%20out%20AMQP%20Agent%20and%20stop%20wasting%20your%20time!%20https%3A%2F%2Fgithub.com%2FMarwanAlsoltany%2Famqp-agent%20 "Tweet")

---

Key Features
------------

[](#key-features)

1. Framework agnostic, integrates easily in any codebase
2. An intuitive and tested API with out-of-the-box support for **Publishers**, **Consumers**, and **RPC Endpoints**
3. Contains tons of helpers to get you up and running in no time without deep knowledge of the topic
4. Unlimited flexibility when it comes to customizing it to your exact needs
5. Actively maintained, well documented and all about syntactic sugar.

---

Installation
------------

[](#installation)

Try AMQP Agent out now:

#### Composer using Packagist:

[](#composer-using-packagist)

```
composer require marwanalsoltany/amqp-agent
```

#### Composer using GitHub Repo (unstable):

[](#composer-using-github-repo-unstable)

Copy this configuration in your `composer.json`:

```
"repositories": {
    "amqp-agent-repo": {
        "type": "vcs",
        "url": "https://github.com/MarwanAlsoltany/amqp-agent.git"
    }
},
"require": {
    "marwanalsoltany/amqp-agent": "dev-dev"
},
"minimum-stability": "dev"
```

Run:

```
composer update
```

[![#ff6347](https://camo.githubusercontent.com/d746d999b149d28b89bd8f0411cc2d3406570d751d30c3fa0d6aa32896b26e98/68747470733a2f2f7669612e706c616365686f6c6465722e636f6d2f31312f6630336331352f3030303030303f746578743d2b)](https://camo.githubusercontent.com/d746d999b149d28b89bd8f0411cc2d3406570d751d30c3fa0d6aa32896b26e98/68747470733a2f2f7669612e706c616365686f6c6465722e636f6d2f31312f6630336331352f3030303030303f746578743d2b) **Note:** *AMQP Agent supports now PHP 7.1 by default starting from version v1.1.1, if you used the `php7.1-compatibility` branch in older versions, update your composer.json!*

---

About AMQP Agent
----------------

[](#about-amqp-agent)

AMQP Agent tries to simplify the implementation of a message-broker in a PHP project. It takes away the entire overhead of building and configuring objects or creating classes that you would need in order to talk with RabbitMQ server (through *php-amqplib*) and exposes a tested, fully configurable, and flexible API that fits almost any project.

The *php-amqplib* library is awesome and works very well. The one and only problem is, it's pretty bare-bone to be used in a project, without remaking your own wrapper classes, it's almost impossible to not write spaghetti code. Plus the enormous amount of functions, methods, and configurations (parameters) that come with it make it really hard to implement a reasonable API to be used. AMQP Agent solves this problem by making as much abstraction as possible without losing control over the workers and by bringing back the terminology associated with message-brokers, a Publisher and a Consumer is all that you need to deal with if you are a newcomer.

According to this motto, AMQP Agent makes working with RabbitMQ as fun and elegant as possible by exposing some fluent interfaces that are cleverly implemented, fit modern PHP development, nice to work with and very simple to use; yet very powerful and can overwrite the smallest quirks at any point of working with the worker. With AMQP Agent you can start publishing and consuming messages with just a few lines of code!

AMQP Agent does not overwrite anything of *php-amqplib* nor it does change the terminology associated with its functions. It only simplifies it; takes out the noise of functions' names and extends it in some places. It also adds some nice features like workers-commands, dynamic channel-waiting, and facilitation methods.

AMQP Agent does also offer a powerful event-based RPC Client and RPC Server for your IoT projects.

Working with AMQP Agent can be as easy as:

```
// Publisher
$publisher = new Publisher();
$publisher->work($messages);

// Consumer
$consumer = new Consumer();
$consumer->work($callback);

// RPC Client
$rpcClient = new ClientEndpoint();
$rpcClient->connect();
$response = $rpcClient->request($request);
$rpcClient->disconnect();

// RPC Server
$rpcServer = new ServerEndpoint();
$rpcServer->connect();
$request = $rpcServer->respond($callback);
$rpcServer->disconnect();
```

---

API
---

[](#api)

AMQP Agent exposes a number of concrete classes that can be directly used and other abstract classes that can be extended. These two class-variants also have a helper sub-division.

#### AMQP Agent Classes

[](#amqp-agent-classes)

ClassDescriptionAPI[AbstractWorker](./src/Worker/AbstractWorker.php) `*A`An abstract class implementing the basic functionality of a worker.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Worker_AbstractWorker.html)[Publisher](./src/Worker/Publisher.php) `*C*S`A class specialized in publishing. Implementing only the methods needed for a publisher.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Worker_Publisher.html)[Consumer](./src/Worker/Consumer.php) `*C*S`A class specialized in consuming. Implementing only the methods needed for a consumer.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Worker_Consumer.html)[AbstractEndpoint](./src/RPC/AbstractEndpoint.php) `*A`An abstract class implementing the basic functionality of an endpoint.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_RPC_AbstractEndpoint.html)[ClientEndpoint](./src/RPC/ClientEndpoint.php) `*C`A class specialized in requesting. Implementing only the methods needed for a client.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_RPC_ClientEndpoint.html)[ServerEndpoint](./src/RPC/ServerEndpoint.php) `*C`A class specialized in responding. Implementing only the methods needed for a server.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_RPC_ServerEndpoint.html)[AmqpAgentParameters](./src/Config/AmqpAgentParameters.php) `*C*H`A class that contains all AMQP Agent parameters as constants.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Config_AmqpAgentParameters.html)[Utility](./src/Helper/Utility.php) `*C*H`A class containing miscellaneous helper functions.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_Utility.html)[Event](./src/Helper/Event.php) `*C*H`A simple class for handling events (dispatching and listening).[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_Event.html)[ArrayProxy](./src/Helper/ArrayProxy.php) `*C*H`A class containing methods for for manipulating and working arrays.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_ArrayProxy.html)[ClassProxy](./src/Helper/ClassProxy.php) `*C*H`A class containing methods for proxy methods calling, properties manipulation, and class utilities.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_ClassProxy.html)[IDGenerator](./src/Helper/IDGenerator.php) `*C*H`A class containing functions for generating unique IDs and Tokens[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_IDGenerator.html)[Serializer](./src/Helper/Serializer.php) `*C*H`A flexible serializer to be used in conjunction with the workers.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_Serializer.html)[Logger](./src/Helper/Logger.php) `*C*H`A class to write logs, exposing methods that work statically and on instantiation.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_Logger.html)[Singleton](./src/Helper/Singleton.php) `*A*H`An abstract class implementing the fundamental functionality of a singleton.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_Singleton.html)[Config](./src/Config.php) `*C*R`A class that turns the configuration file into an object.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Config.html)[Client](./src/Client.php) `*C*R`A class returns everything AMQP Agent has to offer. A simple service container so to say.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Client.html)[Example](./src/Helper/Example.php) `*A*H`An abstract class used as a default callback for the consumer.[Doc](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Helper_Example.html)> See also: [AbstractWorkerSingleton](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Worker_AbstractWorkerSingleton.html), [PublisherSingleton](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Worker_PublisherSingleton.html), [ConsumerSingleton](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Worker_ConsumerSingleton.html), [AbstractWorkerInterface](https://marwanalsoltany.github.io/amqp-agent/interfaces/MAKS_AmqpAgent_Worker_AbstractWorkerInterface.html), [PublisherInterface](https://marwanalsoltany.github.io/amqp-agent/interfaces/MAKS_AmqpAgent_Worker_PublisherInterface.html), [ConsumerInterface](https://marwanalsoltany.github.io/amqp-agent/interfaces/MAKS_AmqpAgent_Worker_ConsumerInterface.html), [WorkerFacilitationInterface](https://marwanalsoltany.github.io/amqp-agent/interfaces/MAKS_AmqpAgent_Worker_WorkerFacilitationInterface.html), [WorkerMutationTrait](https://marwanalsoltany.github.io/amqp-agent/traits/MAKS_AmqpAgent_Worker_WorkerMutationTrait.html), [WorkerCommandTrait](https://marwanalsoltany.github.io/amqp-agent/traits/MAKS_AmqpAgent_Worker_WorkerCommandTrait.html), [AbstractEndpointInterface](https://marwanalsoltany.github.io/amqp-agent/interfaces/MAKS_AmqpAgent_RPC_AbstractEndpointInterface.html), [ClientEndpointInterface](https://marwanalsoltany.github.io/amqp-agent/interfaces/MAKS_AmqpAgent_RPC_ClientEndpointInterface.html), [ServerEndpointInterface](https://marwanalsoltany.github.io/amqp-agent/interfaces/MAKS_AmqpAgent_RPC_ServerEndpointInterface.html), [EventTrait](https://marwanalsoltany.github.io/amqp-agent/traits/MAKS_AmqpAgent_Helper_EventTrait.html), [ArrayProxyTrait](https://marwanalsoltany.github.io/amqp-agent/traits/MAKS_AmqpAgent_Helper_ArrayProxyTrait.html), [ClassProxyTrait](https://marwanalsoltany.github.io/amqp-agent/traits/MAKS_AmqpAgent_Helper_ClassProxyTrait.html), [AbstractParameters](https://marwanalsoltany.github.io/amqp-agent/classes/MAKS_AmqpAgent_Config_AbstractParameters.html).

#### Bibliography

[](#bibliography)

- `*C` **Concrete:** This class is a concrete class and can be instantiated directly.
- `*A` **Abstract:** This class is an abstract class and cannot be instantiated directly.
- `*H` **Helper:** This class is a helper class. Third-party alternatives can be freely used instead.
- `*R` **Recommended:** This class is recommended to be used when working with AMQP Agent (best practice).
- `*S` **Singleton:** This class has a singleton version available via suffixing the class name with `Singleton` and can be retrieved via `*Singleton::getInstance()`, i.e. `Publisher` -&gt; `PublisherSingleton`.

[![#ff6347](https://camo.githubusercontent.com/d746d999b149d28b89bd8f0411cc2d3406570d751d30c3fa0d6aa32896b26e98/68747470733a2f2f7669612e706c616365686f6c6465722e636f6d2f31312f6630336331352f3030303030303f746578743d2b)](https://camo.githubusercontent.com/d746d999b149d28b89bd8f0411cc2d3406570d751d30c3fa0d6aa32896b26e98/68747470733a2f2f7669612e706c616365686f6c6465722e636f6d2f31312f6630336331352f3030303030303f746578743d2b) **Note:** *Singleton is considered an anti-pattern, try avoiding it as much as possible, though there are use-cases for it. Use singletons only if you know what you are doing.*

---

Configuration
-------------

[](#configuration)

If you just quickly want to publish and consume messages, everything is ready and configured already, AMQP Agent is shipped with a tested configuration that follows best practices. You can simply import `Publisher` class and/or `Consumer` class in your file and overwrite the parameters you want (RabbitMQ credentials for example) later on the instance.

If you want to fine-tune and tweak AMQP Agent configuration to your exact needs, there is a bit of work to do. You have to supply a config file (see: [maks-amqp-agent-config.php](./src/Config/maks-amqp-agent-config.php) and pay attention to the comments). You don't have to supply everything, you can simply only write the parameters you want to overwrite, AMQP Agent is smart enough to append the deficiency. These parameters can also be overwritten later through public assignment notation or per method call.

[![#1e90ff](https://camo.githubusercontent.com/586f70f30fb44cfe6604bec8fbc77f81c4910db8159ad925f0c3f923c51d53d2/68747470733a2f2f7669612e706c616365686f6c6465722e636f6d2f31312f3165393066662f3030303030303f746578743d2b)](https://camo.githubusercontent.com/586f70f30fb44cfe6604bec8fbc77f81c4910db8159ad925f0c3f923c51d53d2/68747470733a2f2f7669612e706c616365686f6c6465722e636f6d2f31312f3165393066662f3030303030303f746578743d2b) **Fact:** *AMQP Agent uses the same parameter names as php-amqplib in the config file and in the parameters array passed on the method call.*

#### Here is an example of a config file

[](#here-is-an-example-of-a-config-file)

```
