amuck-landowner

1000th post - RabbitMQ with different programming languages

wlanboy

Content Contributer
So this is my 1000th post.

Hopefully most of them were worth reading.

Looking back to the last year my best find for new servers was the RabbitMQ server.

If anybody wants to play with RabbitMQ he/she can send me a PM I will create him/her a free account on my RabbitMQ server in Dallas.

If you want to play with php it is quite easy:


apt-get update && apt-get install cmake make gcc php5-dev

# Download the rabbitmq-c library
git clone git://github.com/alanxz/rabbitmq-c.git
cd rabbitmq-c
# Enable and update the codegen git submodule
git submodule init
git submodule update
# Configure, compile and install
autoreconf -i && ./configure && make && sudo make install

pecl install amqp
echo 'extension=amqp.so' >> /etc/php5/conf.d/20-amqp.ini
/etc/init.d/php5-fpm reload

And a simple publisher:


<?php

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->exchange_declare('logs', 'fanout', false, false, false);

$data = implode(' ', array_slice($argv, 1));
if(empty($data)) $data = "info: Hello World!";
$msg = new AMQPMessage($data);

$channel->basic_publish($msg, 'logs');

echo " [x] Sent ", $data, "\n";

$channel->close();
$connection->close();

?>

And a simple receiver:


<?php

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPConnection;

$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->exchange_declare('logs', 'fanout', false, false, false);

list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);

$channel->queue_bind($queue_name, 'logs');

echo ' [*]Waiting for logs. To exit press CTRL+C', "\n";

$callback = function($msg){
echo ' [x] ', $msg->body, "\n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);

while(count($channel->callbacks)) {
$channel->wait();
}

$channel->close();
$connection->close();

?>

The examples are from the third tutorial.

Happy coding!
 

Virtovo

New Member
Verified Provider
RabbitMQ is pretty cool.  Currently writing a project using Flask, Celery and RabbitMQ
 

wlanboy

Content Contributer
This rabbitmq messaging is like an IM perhaps?
You can call it like that. RabbitMQ is a big IRC server for programmers :)

The main benefit is that you can do all your calls asynchronous.

So you do not create a worker and call a method:


Worker w1 = new Worker();
Result res = w1.dosomthing();

But you send a message and wait for a response:


Payload pay = new Payload();
pay.addWork(important);

queue.send(pay);

queue.onMessage(dosomething(result));

An example app with JS and RabbitMQ can be found here.
 
Top
amuck-landowner