From a high-level point of view, a publish/subscribe system consists of:
- Publishers that generate messages containing information about a particular subject. The messages are sent to a broker.
- Subscribers that express an interest in messages containing information on a particular subject.
- A broker that acts as a go-between, receiving messages from publishers and compares them to the needs of subscribers. A message is delivered to all subscribers that have expressed an interest in the subject of the message.
This is depicted in the diagram below.
A publisher creates messages and sends them to a broker. Each message encapsulates units of information that reflects information the publisher wants others to know about. The message consists of two distinct parts, a header and the body. The body contains the substance of the message and the header contains information about the message. The header of a message in a publish/subscribe system contains the topic. The topic can be thought of as the subject line of an e-mail, it tells you what the body of the message contains.
The broker can handle messages from many publishers and handles messages on many different topics. The combined set of topics forms a name or information space. The space is hierarchical in nature, with slashes (/) being used to separate the levels. The information space must be carefully designed to help ensure that data is available in a sensible, logical, structure. An example of a simple topic space is:
So the topic for IBM's current price is specified as "finance/stock/ibm/currentprice". When a publisher sends a completed message to a broker, the topic specifies where it belongs in the information hierarchy.
In general, after sending a publication to subscribers, the broker deletes it. However, the publisher can request that the broker keep a copy of the publication, which is then called a retained publication. In this case, the broker remembers the last message that was published on the retained topic. The advantage of retained publications is that applications subscribing to the topic will immediately receive the retained publication and do not have to wait until a publishing application publishes the next message. This allows the application to initialize itself immediately.
Subscribers receive messages from the broker on topics they are interested in. In order to receive messages a subscriber registers an interest in one or more topics with the broker. When registering interest either an absolute topic name or wildcards can be used. For example, a subscriber can specify an interest in the stock price of IBM shares or it can use a wild card and subscribe to the stock prices of all companies.
Multiple subscribers can subscribe for the same topic. When a publisher sends a message that has multiple interested subscribers, the broker will send the message to each subscriber in turn. So one message in can result in many messages out. Once a system has been deployed, new applications can be added at a later date without having to change the applications already deployed. The new applications just subscribe to the existing topics set.
A non-durable subscription is one whose lifetime is limited to the duration of the connection between the subscribing application and the broker. A non-durable subscription is removed from the broker when the application disconnects or when the application explicitly un-subscribes. On the other hand, a durable subscription is one whose lifetime is beyond the duration of the connection between the subscribing application and the broker. A durable subscription is only removed from the broker when the application explicitly un-subscribes. While the application is not connected, matching publication messages are stored in the broker until the application reconnects.
Typically publishers and subscribers are unknown to each other. The broker acts as a matchmaker linking publishers to subscribers. This decoupling makes the system future-proof, it generalizes the use of data, ensuring it is not tied to specific applications. At any time, new applications can be deployed that can make use of existing topics or use a new combination of topics. It is this potential that makes publish/subscribe such a powerful concept.