Zero MQ is a server less message queuing facility that does not require any additional services to work ( ie no MSMQ needing ). I show here an example in how to use that library from C# by creating a small pub subscribe scenario. Just to clarify is a scenario having a publisher sending some messages and one or more subscriber that will be notified of that message.
You can find all the source code for this example here.
In order to get started, we need a wrapper callable for .NET, since ZeroMQ exposes a native interface. I did use clrzmq for the purpose in my project, I did clone my version just to fix some bug in compiling when there is spaces in subdir, so the actual version I use is here.
Then I download the latest stable 3.2.2 RC 2 at the moment I’m writing and launched the setup. The binary after the setup will contain something like this:
Notice the naming containing the –vXXX subfix. this is the C++ Runtime Version the library is built against. Pick the correct one for your system ( you must have a Visual C++ runtime installed ) and rename it as libzmq.dll, since the wrapper expect the dll with this name. Alternatively you can download the wrapper via nuget:
PM> Install-Package clrzmq –Pre
In order to just see the example you can just checkout the example repository.
The example is divided in two (console) application, a publisher and a receiver. Let’s see the sender below:
Really simple, the key point are the SocketType.PUB, meaning we use the socket to publish messages, and the Bind in which we decide an address and a protocol where to listen for connections. Then we start sending some rubbish on the created channel. Message are string, but eventually they are byte. The Send overload accepting a string is actually a wrapper additional bonus.
No server is required in order to dispatch messages
Send is not blocking: independently if there is subscriber or not, Send function exits immediately.
Let’s see the the subscriber:
the key points here are the SocketType.SUB, the subscriber market, the Connect that must match the publisher protocol/address, and the SubscribeAll ( there is a less eager Subscribe that allow to specify a filter for the messages ).
You can start/stop a subscriber at any moment, it will be notified soon of the published messages
Messages are not queued at all.
Last point maybe is a little confusing if you expect something like MSMQ: there is no messages buffer somewhere storing non consumed messages ( ie there is no Permanent Subscriptions as in ActiveMQ ), if you want that feature you must implement it externally.
So, a great and simple library, having the simplicity,lightness no service requirement as a pro,
but the drawback of needing the Visual C++ runtime and the leak of a permanent subscription out of the box.
I had a chance to test the example codebase here on a Widow7 almost clean machine ( without any VisualStudio in ) and the solution works by XCOPY deploying msvcr100.dll and msvcp100.dll, included into the repository. Taths a great thing and it makes the 0 in the 0MQ being an actual 0 :D