Publish and subscribe is a style of messaging in which applications (subscribers) register interest in a particular subject (known as a topic) with an intermediary (a broker). The broker then compares those subscription topics to the topics of messages sent by other applications (publishers) and forwards messages to those subscribers where a match is made.
Each message has a header and a body. The body contains the message content. The header, which is similar to the subject field of an e-mail, describes the content of the message, and includes the message topic.
A single message sent by a publisher may be matched and sent to many subscribers. This messaging style is of particular benefit when multiple components in a system need to receive notification of a given event. For example, a temperature sensor may have many different systems monitoring it for different reasons. The broker decouples the publisher from the subscribers and gives flexibility for the adding or removal of publishers or subscribers without needing to perform application specific integration between each component.
A typical situation would be as follows:
- Each subscriber individually subscribes with the broker to receive messages published to the “Temperature” topic.
- A publication is sent from the publisher to the broker with “Temperature” as its topic.
- The broker checks its internal record of subscriptions and sends the message to each subscriber that has registered to receive messages with the topic “Temperature”.
The following figure shows a simple publish and subscribe application that includes one publisher, one broker, and three subscribers.
Figure 1. A simple publish and subscribe application
A publish and subscribe application may have more than one publisher, more than one subscriber, and even more than one broker. If necessary, an application can be both a publisher and a subscriber to one or more brokers.
Topics and hierarchical topic names
A topic is a character string that describes the data that is published in a publish and subscribe system.
Publication and subscription messages
This section describes terms that you should be familiar with for developing publish and subscribe applications.
Parent topic: Understanding messaging applications: XPD622