The official Fatica Labs Blog! RSS 2.0
# Friday, February 15, 2013

In the last post I shown a Publish/Subscribe communication pattern with ZeroMQ and its C# binding library showing an asynchronous way in dispatching messages through many clients. Well this is not the only way we have. Another strategy is to have a listener and many clients sending messages to it and awaiting for response. All this is achieved by changing the Socket type we create.

You can find the code for this example and the previous one here.


Here the code for the client:


After the connect, we start a loop sending a message to the server, and receiving the reply from it.

Here is the server:


Please note the Bind() function, this is the line saying server is listening for calls. Code contains a test, proving messages are handled in sequence even if many client are sending messages in concurrency. This is part of the key point of this communication pattern:


Request/Reply key Point:

  • Client pass without error the Connect call, even if the server is not yet listening.
  • The client send call is never blocking.
  • The client Receive() blocks until server reply.
  • It is not possible to Send another message if no reply from server is received.
  • Server is guarantee to process one request at a time ( queue )

So there is a sort of state on the cannel, and we have some feedback about the fact the recipient handle our messages. If you heard about the Saga pattern you probably guess when this scenario can be used.

Friday, February 15, 2013 7:12:15 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
C# | communication | ZeroMQ

# Tuesday, February 12, 2013

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.

Notice that:

  • 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 ).

Notice that:

  • 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

Tuesday, February 12, 2013 3:26:11 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
C# | communication | ZeroMQ

My Stack Overflow

Send mail to the author(s) E-mail

profile for Felice Pollano at Stack Overflow, Q&A for professional and enthusiast programmers
About the author/Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2021
Felice Pollano
Sign In
Total Posts: 157
This Year: 0
This Month: 0
This Week: 0
Comments: 140
This blog visits
All Content © 2021, Felice Pollano
DasBlog theme 'Business' created by Christoph De Baene (delarou) and modified by Felice Pollano