What Is ZeroMQ
ZeroMQ is a C library that contains a bunch of insane sockets, that provide a very very cool abstraction over the typical socket code you would find yourself writing. It provides building blocks by way of a standard set of sockets that have been built with certain scenarios in mind.
The people that make it were instrumental in the advanced message queue protocol (AMQP) being written, and are very big in the messaging world.
There is a completely awesome book that everyone should read, it is by Pieter Hintjens
There is also a online version of the PDF book that has full code samples, which is known as the guide:
Why Use ZeroMQ/Messaging At All
If you have ever written any asynchronous code, and have had to deal with shared state, and have had to deal with that, you will know that that brings locks/semaphores etc etc.
Now imagine a world where you don’t care about locks, semaphores etc etc, you just pass a message, and there is no shared state to worry about. Welcome to messaging. That is how you can write systems with a high throughput of messages per second, without the overhead of shared state management.
Zero is at its core a messaging library. It can be setup in a brokerless manner or also used broker, or even peer to peer. It is sockets that make it powerful. They are the fundamental building blocks, which you may use to create large distributed architectures, or very small ones.
I would urge you all to read the book, or check out the guide, they have changed the way I think about certain tasks for sure.
Where Do I Get The ZeroMQ library?
First things first, I mentioned that ZeroMQ is written in C, but has many many language bindings. There is in fact a C# binding that you could use, which is the zmqcli binding. The thing with that, is that the errors you get are quite confusing at times, as it has a tendency to show the actual C error code.
I wanted (if possible) to use an entirely native port of ZeroMQ, luckily for me there is just such a thing, by way of the NetMQ project. That is the library I will be using throughout all my posts.
You can install NetMQ using the following Nuget package manager command line:
So without further ado lets get down to see a very simple example.
What Does The Example Do
The example is dead straight forward, we send a message from the client to the server, and the server sends a message back. This is a (extremely simple) example of the Request/Response pattern, of which we will see a lot more examples. Zero also supports publish/subscribe which we will look at too (though not in as much detail as request/response).
So let’s see some code shall we:
Believe it or not that code is enough to have a fully functioning request (client) / response (server) pattern, up and working. Don’t believe me here is some output to prove it
Ok, so it does work, that is quite mad. So how does it work?
Well there are a couple of take away points there
- We are able to create a request/response pattern, by using specialized sockets that are geared towards working in the request/response scenario.
- We can use tcp as the protocol (ZeroMQ also supports others such as inproc)
- We did not have to spin up any extra thread on the server to deal with the freshly connected client socket and then continue to accept other client sockets. In fact this code could pretty much talk to 1000nds of clients without much alteration at all (in fact I will show you an example of the using separate processes)
- There is this magical NetMQContext. This is mandatory and must be used whenever you use ZeroMQ sockets.
There are a couple of specific things to talk about in request/response, in that those type of sockets are assumed to be 1:1 request/response. So If you call ReceiveString() twice on the clients request socket without the servers response socket sending something you will get an Exception, as can be seen in the screen shot below.
Running In Separate Threads
This demo is to show you how you could use an internal processes messaging system. You obviously need to use new threads in this example as we need to not block on the receive() method of the sockets
This just kicks of a few clients for the server to deal with
Running In Separate Processes
Here is the code running in separate processes, where it can be seen that we did not have to do a damn thing to change the server code at all we just moved it around to run in a new process rather than a new thread.
Where Is The Code For This Article?
The code for all these posts is hosted in one large solution in github: