I am new to ZeroMQ
, I have seen a few examples, seems always involves the listener to do busy loop to receive the message, for example, in the pub/sub
pattern. the receiver will need to do this to consume the message,
while (true)
{
var message = socket.Recv(Encoding.UTF8);
message.Dump();
}
Is there any kind of callback mechanism, like the receiver will just register itself to an socket, and whenever there is a message it will be called, instead of busy looping to check the message.
Depends on your language choice. If, for example, you use node.js, everything is callbacks.
You don't specify your language choice, or what's going on in your application. Typically your strategy will depend on the overall nature of your application.
The reason you'll typically see a loop that is solely focused on receiving messages is twofold:
That said, if you can define some larger loop in your application that you can receive messages once or only periodically during, you can just delegate the task to that larger loop. Say, something like (psuedocode):
... that way you're processing all queued messages once per functional loop, rather than having a loop solely dedicated to your messages.
Whether or not you can use a callback system is largely dependent upon your language and ZMQ binding.