ZeroMQ on Raspberry Pi to bind your systems

Warning : This article has been automatically translated by Bing Translate

We already saw how install the message broker RabbitMQ. Here you will be able to install the broker that goes, multi platform : ZeroMQ.

logo

The objectives of a provider of messages, There is a beautiful theory :

  • Simplify transport
  • Deliver a message to one or more recipients
  • Request information from a remote server
  • Subscribe to publications
  • Transform messages for the interoperability of systems

What is nice with ZeroMQ is that in addition to offer these features, It is cross-platform (Windows, Mac, Linux, iOS, Android, etc.), can be used in most of 40 programming languages, is quick and can run on systems with little memory. So the Raspberry Pi ! And it is far worth the price of an IBM Websphere Integration Bus since it is Open Source !

To fully understand all the features offered by ZeroMQ, I invite you to watch This link which explains the main features.

ZeroMQ-Functions

I therefore propose to set up a client-server simple to illustrate the use of ZeroMQ. So let's install the broker on the Raspberry Pi initially. It will be our server :

  1. Update the system
    apt-get update && apt-get upgrade -y
  2. Install the packages needed for compiling
    apt - get install –y g    make autoconf automake libtool
  3. Download the latest version (4.0.3 at the time of writing)
    wget http://Download.zeromq.org/zeromq-4.0.3.tar.gz
  4. Unpack the archive
    tar zxvf zeromq - 4.0.3.tar.gz
  5. Compile and install ZeroMQ
    cd zeromq-4.0.3 ./configure make make install ldconfig

You here with your ready to use ZeroMQ. Or almost. Remains to implement the program to manage your trade.

I propose a simple server in Python

  1. Side Raspberry Pi, the server, We will install the “binding” in Python, so in fact the library which will allow you to use ZeroMQ via Python programs.
    apt - get install python-pip pip install pyzmq python-all-dev
  2. Here's the source of the program which will be the server. Alors j’espère que les commentaires sont assez clairs :
    #!/usr/bin/env python
    
    # Import basic packages import os import sys import time
    
    # Import ZeroMQ package import zmq  print "Starting Pi Home Server ZeroMQ server"
    # Initialize the ZeroMQ context context = zmq.Context()
    
    # Configure ZeroMQ to send messages zmq_send = context.socket(zmq.PUB)
    # The communication is made on socket 1111
    zmq_send.bind("tcp://*:1111")
    print "Sending messages on tcp://*:1111"
    
    # Configure ZeroMQ to receive messages zmq_recv = context.socket(zmq.SUB)
    # The communication is made on socket 1112
    zmq_recv.bind("tcp://*:1112")
    zmq_recv.setsockopt(zmq.SUBSCRIBE, '')  # subscribe to everything print "Receiving messages on tcp://*:1112"
    
    print "Pi Home Server ZeroMQ server running !"
    While True:
        message = None     answer = None
    
        # Command to wait for an incoming message     try:
            # Capture the message and store it         message = zmq_recv.recv()
            print "Message received : " + message     except zmq.ZMQError as err:
            print "The server received the following error message : " + str(err)
    
        # A message has been received     if message:
            answer = str(int(message) * 2)
            try:
    	    print "Sending answer : " + answer             zmq_send.send(answer)
            except zmq.ZMQError as err:
                print "Error while trying to send the answer : " + str(err)
    
    # Never reached in theory ...
    zmq_send.close()
    zmq_recv.close()
    context.term()
    print "The server has been closed !"

Side client, I chose to do it on my Mac, in Python still (Java I have not managed to compile it as I wanted to). For this :

  1. Install Python
  2. Install PyZMQ
    sudo easy_install pyzmq
  3. Et voici la partie client :
    #!/usr/bin/env python  import os import sys import time  import zmq  context = zmq.Context()
    
    # Configure ZeroMQ to send messages zmq_recv = context.socket(zmq.SUB)
    # The communication is made on socket 1111
    zmq_recv.connect("tcp://192.168.1.33:1111")
    
    # Configure ZeroMQ to receive messages zmq_send = context.socket(zmq.PUB)
    # The communication is made on socket 1112
    zmq_send.connect("tcp://192.168.1.33:1112")
    zmq_recv.setsockopt(zmq.SUBSCRIBE, '')
    
    print "Pi Home Server ZeroMQ client running !"
    
    While True:
        print "Input value to send : "
        message = raw_input().strip()
    
        if message:
            try:
                print "Sending value : " + message zmq_send.send(message)
            except zmq.ZMQError as err:
                print "Error while trying to send the value " + message + " : " + str(err)
    
        try:
            incoming_message = zmq_recv.recv()
            print "Value received from the server : " + incoming_message except zmq.ZMQError as err:
            print ' Receive error: ' + str(err)

Therefore the server expects a value, multiplies it by 2 and returns the result to the client. Yes this is very useful I know … C’est l’intention qui compte 🙂

ZeroMQ client

The result of the client ZeroMQ

ZeroMQ server

The result of the server ZeroMQ

So this is the main principle. This system can be very convenient to connect your systems, centralize treatments, etc. It's fast and really easy to implement.

Do not hesitate to share your ideas of uses at home or even in a professional setting !

Other info and examples here.

Like this article ? Bear with me on patreon!

You may also like...