I recently read about ZeroMQ and got hooked on it. So I decided to try it out by using Scala. This is just a simple "Hello ZMQ from Scala" tutorial to be run on OS X (El Capitan). It requires the libzmq to be installed and to have either the Java binding (JZMQ) or the Scala binding installed too. I am going to use for this post the JZMQ binding.


The first thing that must be done is to make zeromq library from source code. In order to so, you must ensure you have some dependencies installed, that can be installed using Homebrew in case of not having them:

  • brew install libtool
  • brew install pkg-config
  • brew install autoconf
  • brew install automake

Make libzmq library

You should grab the source code and extract it. We are going to use the latest at the time of writing this post (v4.1.4).

    mkdir zeromq
    cd zeromq
    curl -O http://download.zeromq.org/zeromq-4.1.4.tar.gz
    tar zxf zeromq-4.1.4.tar.gz
    cd zeromq-4.1.4

Then, it should be configured. I am excluding libsodium library because I have not installed it neither I will need it for prototyping purposes:

    ./configure --without-libsodium

Finally, to install it system-wide:

    sudo make install

That will install the library (libzmq.dylib) in /usr/local/lib and the header files in /usr/local/include. You can now remove the folder zeromq-4.1.4 if you like.

    cd ..
    rm -rf zeromq-4.1.4

Installing JZMQ Binding

Clone the git repo first:

    git clone https://github.com/zeromq/jzmq.git
    cd jzmq

Make the binding this way:

    sudo make install

That will install the library (libjzmq.dylib) in /usr/local/lib and will copy zmq.jar file in /usr/local/share/java. You can now remove the folder jzmq if you like.

    cd ..
    rm -rf jzmq

Create the "Hello ZQM" application

We have already installed all the requisites for being able to write and run (assuming you also have JVM installed) our first Scala project on top of ZMQ. I have create a GitHub repository where you can find the project. It is a sbt project so you should have it installed. Otherwise, you can install it right now by using brew:

    brew install sbt

If you typed the commands as they appeared in the tutorial, your working folder should be zeromq. This is not important though but this way it keeps things ordered. Now, clone the git repository and run the client and server. You should open two shell sessions in order to run each of them simultaneously. I am using iTerm as the terminal application although you can use the one that Apple ships.

On a first shell script in the zeromq working dir we should clone the git repository and also run sbt:

    git clone https://github.com/oscar-martin/Hello0MQ.git
    cd Hello0MQ
    sbt -Djava.library.path=/usr/local/lib

It is important to note that we are passing a JVM property (java.library.path) to indicate where to load the library that will be requested to load when ZeroMQ is run. The location is where the command sudo make install in the libzmq step placed it.

On a second shell script, you need to point to zeromq/Hello0MQ and also launch sbt:

    cd zeromq/Hello0MQ
    sbt -Djava.library.path=/usr/local/lib

So both shell sessions are showing the sbt prompt. You can list the projects. In any of these sessions, type:

    > projects

And the output should look like this:

[info]     client
[info]   * root
[info]     server

which indicates root is current project. So you should change the current project in both shell sessions:

In the first shell session:

    > project server

In the second shell session:

    > project client

Finally, in order to run our "Hello ZMQ from Scala" application, you should just run it, this way:

In the first shell session (server):

    > run

In the second shell session (client):

    > run localhost

It will compile each project and run it. If succeeded, the output should be like this:

In the first shell session (server):

[info] Running Main
Listening on port 5555...
Received request: [Hello ZQM]
Received request: [Hello ZQM]
Received request: [Hello ZQM]
Received request: [Hello ZQM]
Received request: [Hello ZQM]
Received request: [disconnect]
Received 'disconnect' command.

In the second shell session (client):

[info] Running Main
Connected to Server on localhost:5555
Sending request 1: [Hello ZQM]
Received reply 1: [from Scala!]
Sending request 2: [Hello ZQM]
Received reply 2: [from Scala!]
Sending request 3: [Hello ZQM]
Received reply 3: [from Scala!]
Sending request 4: [Hello ZQM]
Received reply 4: [from Scala!]
Sending request 5: [Hello ZQM]
Received reply 5: [from Scala!]

Once execute, you should exit the sbt project in each session in order to run it again. Otherwise, you will have an “UnsatisfiedLinkError: Native Library already loaded in another classloader error.

So I hope you have been able to launch it without any problem. I do not really like to install in the Mac a lot of different software packages just for prototyping purposes. Then, I am going to start working on an easier approach: to prepare a Docker image with all the requisites on it and to use it to run my Hello0MQ project on it.