This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A simple abstraction for RabbitMQ

v0.3.1 2016-08-07 22:04 UTC

This package is auto-updated.

Last update: 2021-09-29 01:50:16 UTC


SensioLabsInsight Scrutinizer Code Quality Code Coverage Code Climate Build Status Latest Stable Version License

A simple abstraction for RabbitMQ.

Carrot aims to make it easy to get started with RabbitMQ and PHP, at the same time maintaining the flexibility to implement all the supported messaging patterns.


You must have a rabbitmq server running to use this package. For more on this refer RabbitMQ documentation.


With Composer

$ composer require sunspikes/carrot


To configure a RabbitMQ connection

    'host' => '',
    'port' => 5672,
    'username' => 'guest',
    'password' => 'guest',
    'vhost' => '/'

To publish message to RabbitMQ,

// Create producer which sends messages to 'test-exchange'
$producer = Carrot::producer('test-exchange');

// Send a message to 'TestQueue'
$producer->send('TestQueue', [
    'text' => 'hello',

To Consume messages from RabbitMQ

// Create a consumer which reads from 'test-exchange'
$consumer = Carrot::consumer('test-exchange');

// Add a listener for for queue 'TestQueue', the callback will be executed
// everytime there is a new message on this queue, which will print 'hello'
$consumer->add('TestQueue', function($message) {
    print $message->text;

// Listen for messages on 'test-exchange', this is a wait loop which will keep the
// consumer running till it's manually terminated or the connection is lost

More on usage

You don't have to use static methods to build and use producers and consumers, also the library makes it easy to switch between different exchange types.

For example, you can also create a direct exchange & produce messages to it like,

$config = [
    'host' => '',
    'port' => 5672,
    'username' => 'guest',
    'password' => 'guest',
    'vhost' => '/'

$carrot = new Carrot('test-exchange', 'direct', $config);
$producer = $carrot->getProducer();

$producer->send('TestQueue', [
    'text' => 'hello'

Also the consumer can be created and listened to a direct exchange for messages like,

$carrot = new Carrot('test-exchange', 'direct', $config);
$consumer = $carrot->getConsumer();

$consumer->add('TestQueue', function($message) {
    print $message->text;


By default carrot producer will automatically serialize the message and consumer will deserialize the message acknowledge the incoming messages, you could disable this delegation by setting the configuration parameter delegate to false

// make some message
$message = ['text' => 'hello'];

// serialize it manually
$message = json_encode($message);

// send the message
$producer->send('TestQueue', $message);

// At consumer end, handle the message manually
$consumer->add('TestQueue', function (AMQPMessage $message) use ($consumer) {
    $decoded = json_decode($message->body);
    //... do something with $decoded->text
    // if everything went fine acknowledge message
    // if something went wrong with the message reject message, this will discard the message (optionally requeue the message)
    $consumer->rejectMessage($message, $requeue = false);


You can run the examples from the /example folder

First run the consumer, it will create the exchange and queue on the rabbitmq server

$ php example/consumer.php

[*] Consumer starting to listen for messages from rabbitmq...
[*] Received message: hello

As soon as you run the producer, the consumer will print the message text sent by the producer

$ php example/producer.php

[*] Producer started...
[*] Producer sent message to rabbitmq


Run phpunit


Krishnaprasad MG [@sunspikes]


Please feel free to send pull requests.


This is an open-sourced software licensed under the MIT license.