Skip to content

Instantly share code, notes, and snippets.

@hkulekci
Last active October 4, 2018 10:46
Show Gist options
  • Save hkulekci/6087182 to your computer and use it in GitHub Desktop.
Save hkulekci/6087182 to your computer and use it in GitHub Desktop.
Rabbit MQ and PHP
<?php
/**
* Filename: php-receive.php
* Purpose: Receive messages from RabbitMQ server using AMQP extension
* Exchange Name: exchange1
* Exchange Type: fanout
* Queue Name: queue1
*/
$connection = new AMQPConnection();
$connection->setLogin("admin");
$connection->setPassword("password");
$connection->connect();
if (!$connection->isConnected()) {
die('Not connected :('. PHP_EOL);
}
// Open channel
$channel = new AMQPChannel($connection);
// Open Queue and bind to exchange
$queue = new AMQPQueue($channel);
$queue->setName('queue1');
$queue->bind('exchange1', 'key1');
$queue->declare();
// Prevent message redelivery with AMQP_AUTOACK param
while ($envelope = $queue->get(AMQP_AUTOACK)) {
echo ($envelope->isRedelivery()) ? 'Redelivery' : 'New Message';
echo PHP_EOL;
echo $envelope->getBody(), PHP_EOL;
}
<?php
/**
* Filename: php-send.php
* Purpose: Send messages to RabbitMQ server using AMQP extension
* Exchange Name: exchange1
* Exchange Type: fanout
* Queue Name: queue1
*/
$connection = new AMQPConnection();
$connection->setLogin("admin");
$connection->setPassword("password");
$connection->connect();
if (!$connection->isConnected()) {
die('Not connected :(' . PHP_EOL);
}
// Open Channel
$channel = new AMQPChannel($connection);
// Declare exchange
$exchange = new AMQPExchange($channel);
$exchange->setName('exchange1');
$exchange->setType('fanout');
$exchange->declare();
// Create Queue
$queue = new AMQPQueue($channel);
$queue->setName('queue1');
$queue->declare();
$message = $exchange->publish('Custom Message (ts): '.time(), 'key1');
if (!$message) {
echo 'Message not sent', PHP_EOL;
} else {
echo 'Message sent!', PHP_EOL;
}

Rabbit MQ Installation

> echo "deb http://www.rabbitmq.com/debian/ testing main" > /etc/apt/sources.list.d/rabbitmq.list
> wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
> sudo apt-key add rabbitmq-signing-key-public.asc
> apt-get update
> apt-get install rabbitmq-server -y
> service rabbitmq-server start
> rabbitmq-plugins enable rabbitmq_management
> rabbitmqctl add_user admin password
> rabbitmqctl set_user_tags admin administrator
> rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
> rabbitmqctl delete_user guest
> service rabbitmq-server restart

Make sure you change the word "password" to the actual password you want to login with in the add_user line before running. The default port for the admin interface is 15672. You can use the management interface to finish off your configuration such as adding exchanges, queues, users and vhosts. Make sure to open the correct firewall ports (15672, 5672) if you are going to be accessing the box outside of a VPN or localhost. Reference : http://marcqualie.com/2012/12/install-rabbitmq-on-ubuntu-12.04

After that,

> wget http://pecl.php.net/get/amqp-1.2.0.tgz
> tar -xzf amqp-1.2.0.tgz
> cd amqp-1.2.0
> phpize && ./configure --with-amqp && make && sudo make install

Now we can try following example on our server.

> wget https://gist.github.com/hkulekci/6087182/raw/69e463cd83df9b2db0b7a524dbe17895fbe0c2f6/php-receive.php
> wget https://gist.github.com/hkulekci/6087182/raw/3c0586ad197cec293681f72276f9a1e8201ebcef/php-send.php
> php -f php-send.php
Message sent!
> php -f php-receive.php
New Message
Custom Message (ts): 1374827679

Thats all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment