The publisher-subscriber pattern is the simplest communication mechanism. It is included for pedagogical purposes only. Here, multiple consumers receive messages from one producer. More...
Classes | |
class | UMPS::Messaging::PublisherSubscriber::Publisher |
A ZeroMQ publisher. More... | |
class | UMPS::Messaging::PublisherSubscriber::PublisherOptions |
Options for initializing the publisher in the PUB/SUB pattern. More... | |
class | UMPS::Messaging::PublisherSubscriber::Subscriber |
The subscriber in a publisher/subscriber messaging pattern. More... | |
class | UMPS::Messaging::PublisherSubscriber::SubscriberOptions |
Options for initializing the subscriber in the PUB/SUB pattern. More... | |
The publisher-subscriber pattern is the simplest communication mechanism. It is included for pedagogical purposes only. Here, multiple consumers receive messages from one producer.
This is the most naive messaging strategy available in UMPS. All that happens are that messages are sent from a producer to consumers. This pattern exists for educational and testing purposes and should not be used in production code.
Despite its simplicity the pub-sub pattern is important. From an application standpoint, it is the mechanism that will allow UMPS to distribute data packets, heartbeat messages, pick messages, earthquake messages, etc. This pattern also will allow me to introduce preliminary concepts like the distinction between connecting and binding.
In this example, a publisher will send a handful of text messages to three subscribers.
The important points in this code snippet are that the publisher binds to an endpoint, defines a message to send, then sends the message in a non-blocking way. Notice that the publisher does not care whether or not the subscriber receives the messages.
The important points are that the subscriber connects to an endpoint, receives a pre-agreed upon number of messages, and returns. It is important to consider the blocking (waiting) behavior of the subscriber. If the default is to block indefinitely until a message is received then in this example the program may hang. For this reason, we actually connect the subscriber to the publisher before the publisher is created. You may want to pause for a moment to let that sink in - ZeroMQ allows the subscriber to connect prior to the publisher binding to the socket.
Here is the example driver code that launches this example. We create four threads - a publisher and three subscribers. The subscribers connect first then will block until a message is received. Then the publisher is started. Since this code is brittle, the publisher code pauses after initialization to deal with something called the slow-joiner problem (basically, ZeroMQ has to do some asynchronous work behind the scenes that takes time). In practice, we do not pause since we never send a predefined number of messages in a pub-sub pattern.
The pub-sub is a foundational pattern in messaging that is provides a natural solution for handling streaming data. Additionally, the concepts of binding and connecting were introduced. Despite this, the alert reader is likely asking - what if I have multiple producers? The answer to that question is the Extended Publisher-Subscriber pattern.