Simple ZeroMQ on Ubuntu
December 5, 2010
This blog post covers my initial proof on concept development using ZeroMQ. This is on an Ubuntu 10.10 system.
Installation
I downloaded zeromq-2.0.10.tar.gz and unpacked it. I then had to install the following:
$ sudo apt-get install libtool autoconf automake $ sudo apt-get install uuid-dev g++
After that, I could build and install ZeroMQ using the following three commands:
$ ./configure $ make $ sudo make install $ sudo ldconfig
You should be able to successfully run zmq_queue after install:
$ zmq_queue usage: zmq_queue <config-file>
Python Bindings
We're going to be working in python for this example, so we need to get hold of the python bindings for zeromq. We'll install them using easy_install:
$ sudo easy_install pyzmq
Testing
Here's a simple server and client programme to test with.
The server listens on a local port and prints out what it receives:
import zmq context = zmq.Context() socket = context.socket(zmq.PULL) socket.bind("tcp://127.0.0.1:5000") while True: msg = socket.recv() print "Received %s" % msg
The client simply pushes a Hello World string through to the server:
import zmq context = zmq.Context() socket = context.socket(zmq.PUSH) socket.connect('tcp://127.0.0.1:5000') socket.send("Hello World!")
Running the server then the client twice gives the following output on the server console:
$ python server.py Received Hello World! Received Hello World!
Here we're using the Pipeline pattern, similar in concept to what we did with AMQP and RabbitMQ in previous posts. This is controlled by the use of PUSH and PULL constants passed when we created the socket. See http://api.zeromq.org/zmq_socket.html for details of other patterns available.