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.