Butter AMQP
, (*1)
Butter AMQP is a client library for AMQP protocol purely implemented in PHP. It has no dependencies on any PHP extension
nor other PHP packages. It's very light-weight and lightning fast., (*2)
Butter AMQP supports all base AMQP features and RabbitMQ extensions, including: exchange to exchange bindings,
publisher acknowledgments, negative acknowledgements and others., (*3)
Key features
- Pure PHP implementation of AMQP protocol: no special requirements for PHP and easy upgrade using just composer
- Easy to use functional API, it hides implementation details and reduce risk of making mistake
- Code generator for frame encoding and decoding helps achieve high performance and low memory usage
- Clean design makes it pleasure to work with AMQP, easy to tests and understand
- Full support for AMQP protocol version 0.9.1 and RabbitMQ extensions
Installation
Easiest way to start using Butter AMQP library is to install it using composer.
It has almost no dependencies and does not conflict with any other library., (*4)
Open a command console, enter your project directory and execute the following command to download the latest version of this library., (*5)
$ composer require skolodyazhnyy/butter-amqplib dev-master
This command requires you to have Composer installed globally, as explained in the installation chapter
of the Composer documentation., (*6)
Usage
If you are new to AMQP, I suggest you have a look into RabbitMQ tutorial which
explains all features of AMQP protocol in only 6 chapters tutorial! I have adopted it with code samples for Butter AMQP., (*7)
Every code snippet below extends previous one., (*8)
Connecting to the server
Establish connection to the server and open a channel., (*9)
use ButterAMQP\ConnectionBuilder;
$connection = ConnectionBuilder::make()
->create("//guest:guest@localhost/%2f");
$channel = $connection->channel(1);
Read more, (*10)
Define topology
Declare exchanges and queues., (*11)
use ButterAMQP\ExchangeInterface as Exchange;
use ButterAMQP\QueueInterface as Queue;
$channel->exchange('butter')
->define(Exchange::TYPE_FANOUT, Exchange::FLAG_DURABLE);
$channel->queue('butter')
->define(Queue::FLAG_DURABLE | Queue::FLAG_EXCLUSIVE)
->bind('butter');
Read more, (*12)
Publishing messages
Publish a message to newly declared exchange and it will be delivered to the queue., (*13)
use ButterAMQP\Message;
// Construct a message to be published
$message = new Message('hi there', ['content-type' => 'text/plain']);
// Publish message to default exchange, with routing key "text-messages".
$channel->publish($message, '', 'text-messages');
Read more, (*14)
Consuming messages
Receive your message and acknowledge its delivery., (*15)
use ButterAMQP\Delivery;
// Declare consumer
$consumer = $channel->consume('text-messages', function(Delivery $delivery) {
echo "Receive a message: " . $delivery->getBody() . PHP_EOL;
// Acknowledge delivery
$delivery->ack();
});
// Serve connection until consumer is cancelled
while($consumer->isActive()) {
$connection->serve();
}
Read more, (*16)
Close connection
Properly closing connection to the server will guarantee all temporary queues will be deleted and resources released., (*17)
You don't need to close channels, just connection will be enough., (*18)
$connection->close();
Known issues
- [ ] Decimal type is not supported
- [ ] Unsigned long long type is not supported