NetMQ Lesson #1 – Basics

So you probably already know that NetMQ is port of ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) to .Net.

If you are not familiar with ZeroMQ or NetMQ please read my previous post about introducing NetMQ.

Transport

In NetMQ you can choose from different transport, in this post I will focus on TCP.

  • TCP – communication over the network.
  • INPROC – in process communication, or inter-thread communication.
  • PGM – reliable multicast implemented with Microsoft implementation of PGM.
  • IPC – inter process communication, on NetMQ it’s exactly the same as TCP.

Patterns

As with transports NetMQ comes with different communication patterns, in this post I will cover request-response pattern.

  • Request/Response – client send request and server answer with response.
  • Pub/Sub – client subscribe for messages and server distribute message to all subscribed clients.
  • Dealer/Router – in simple words its client-server communication, dealer is the client and the router is the server, more about this in the future.
  • Push/Pull – one peer push messages and one or more workers peek those messages, good to build a pipeline.
  • Pair – one to one connection, for use with INPROC transport.
  • Dealer/Dealer, Router/Router – more advanced patterns for some cases.

Multi-threading

When you are entering the world of high performance systems (like trading platforms) you soon find out that everything you learned about multi-threading is wrong.

When you are writing high performance systems you don’t use locks, not reader writer lock, not mutex, not monitor and not any other type of lock. If you are writing very good code you even not using .net concurrent collection (except blocking collection, which is one pattern you can use) or interlocked. Now there is very easy way to do it, just don’t share data between threads, but this is another topic worth posting about.

The reason I’m telling you this is that NetMQ (and ZeroMQ) is coming from that world, you are not sharing the socket (which I explain later what is) between threads, the socket is belonging to the thread that create it. If you want to pass data to another thread you can use NetMQ with INPROC as transport (or blocking collection or ring buffer or any other inter-thread communication library). The topic of multi-threading is covered very good in the ZeroMQ guide.

Socket like API

So the basic of NetMQ is the socket object, no matter how many clients you have connected you have one socket, and not matter to how may servers you are connected to you have one socket. When you want to send a message you just call the Send method and when you want to receive a message you just call the Receive message.

Code

So let’s start with simple request response client server example (you need to add a reference to NetMQ, you can find the library on nuget):

  class Program
  {
    static void Main(string[] args)
    {
      using (NetMQContext context = NetMQContext.Create())
      {
        Task serverTask = Task.Factory.StartNew(() => Server(context));
        Task clientTask = Task.Factory.StartNew(() => Client(context));
        Task.WaitAll(serverTask, clientTask);
      }
    }

    static void Server(NetMQContext context)
    {
      using (NetMQSocket serverSocket = context.CreateResponseSocket())
      {
        serverSocket.Bind("tcp://*:5555");

        while (true)
        {
          string message = serverSocket.ReceiveString();

          Console.WriteLine("Receive message {0}", message);

          serverSocket.Send("World");

          if (message == "exit")
          {
            break;
          }
        }
      }
    }

    static void Client(NetMQContext context)
    {
      using (NetMQSocket clientSocket = context.CreateRequestSocket())
      {
        clientSocket.Connect("tcp://127.0.0.1:5555");

        while (true)
        {
          Console.WriteLine("Please enter your message:");
          string message = Console.ReadLine();
          clientSocket.Send(message);

          string answer = clientSocket.ReceiveString();

          Console.WriteLine("Answer from server: {0}", answer);

          if (message == "exit")
          {
            break;
          }
        }
      }
    }
  }

Remember what I told you about not sharing anything between threads? So you are actually allowed to share the NetMQContext between threads. It’s the only NetMQ object that you allowed to share between threads.

So as you can see using NetMQ is pretty easy. One important thing to note, string is not the only thing you can send, NetMQ is actually about delivering binary data, the Send method that receives string is just another overload of the receive method, more usually you will pass byte array and do the object serialization your self or with another library.

You can create different application for the client and for the server and launch multiple clients, in the request response pattern you don’t have to specify which client you sending the response to, the socket just know. In more advanced scenario (dealer/router pattern) you can specify to which client you are sending the message.