Notes on RabbitMQ

So you’ve got a lot of raspberry pi, way more slices of it than you can eat, and you want them to ‘talk’ to each other.

This feat sounds easy enough. I’m sure other people have it taken care of right? Not exactly, but RabbitMQ is a solution for some situations. Without getting too complicated RabbitMQ is a system for sending messages between multiple raspberry pi. There is a message creator, that sends a message to a message server, then a message listener does something based on what it hears.

First task is setting up a RabbitMQ message broker on one of the raspberry pi. This task can be done on all machines or just one. It depends on exactly what you’re planning on doing. You can do one central server, or have them all have their own server.

install some programs and some python libraries

sudo apt-get install python-setuptools git-core erlang logrotate
sudo easy_install pip
sudo pip install --upgrade pip
sudo pip install pika

The typical install might be fixed in the future, but it installs an old version that doesn’t work.

More than likely you’re going to have to do this first:

echo "deb http://www.rabbitmq.com/debian/ stable main" | sudo tee -a /etc/apt/sources.list

Maybe this will work:

sudo apt-get install rabbitmq-server

And grabbing the package is the backup way of doing the same thing.

wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.5.3/rabbitmq-server_3.5.3-1_all.deb
sudo dpkg -i rabbitmq-server_3.5.3-1_all.deb

Somehow this always ends up in my notes, it helps somehow in the pattern of installs.

sudo apt-get -f install

Uncomment the limit line in this file

sudo nano /etc/default/rabbitmq-server

If you want to see what it is doing turn on the web interface for watching:

sudo rabbitmq-plugins enable rabbitmq_management

Remove the default ‘guest/guest’ user

sudo rabbitmqctl delete_user guest

Make a new user and give it a password:

sudo rabbitmqctl add_user <username> <password>
sudo rabbitmqctl set_user_tags <username> administrator
sudo rabbitmqctl set_permissions -p / <username> ".*" ".*" ".*"

For some reason this enables boot defaults:

sudo update-rc.d rabbitmq-server defaults

Rebooting might be a good idea here, and restarting this takes quite a long time

sudo service rabbitmq-server restart

Now all we need to do is run some python on each of the machines on reboot and they will be able to ‘talk’ to each other. There are 3 roles Broker, Producer, and Consumer. However, a broker can also be a consumer or a producer if configured correctly.

This link is very helpful: https://www.rabbitmq.com/getstarted.html

There is some fuzz out there about celery being very useful here. I read up on it some, but it doesn’t appear to absolutely necessary to get things working. It appears that celery might add some kind of additional options to the whole ‘talking’ thing.

http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html

Generally you just have this run in the cron on boot on both the producer and the consumer, and just comment out the other.

 

#### The file
#RabbitMQ.py
#!/usr/bin/env python

import pika

#uncomment this for consumer
#import os

#The debugger
#import logging
#logging.basicConfig()

#The connection
credentials = pika.PlainCredentials(username='user', password='pass')
connection = \
        pika.BlockingConnection(pika.ConnectionParameters('192.168.1.1', 5672, '/', credentials))
channel = connection.channel()
channel.queue_declare(queue='channelname')

#Producer
channel.basic_publish(exchange="", routing_key='channelname', body='A Message')
connection.close()

#Consumer
def callback(ch, method, properties, body):
        os.system("<script>")
channel.basic_consume(callback,
                     queue='channelname',
                     no_ack=True)
channel.start_consuming()