NServiceBus has a built in implementation of the Publish-subscribe pattern.
publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers. Instead, published messages are characterized into classes, without knowledge of what, if any, subscribers there may be. Similarly, subscribers express interest in one or more classes, and only receive messages that are of interest, without knowledge of what, if any, publishers there are.
Or in simpler terms
Subscribers let the publisher know they're interested, and the publisher stores their addresses so that it knows where to send which message.
Depending on the features provided by a given transport there are two possible implementations of Publish-Subscribe mechanics: message-driven (persistence-based) and native.
Message-driven publish-subscribe is controlled by subscribe and unsubscribe system messages sent by the subscriber to the publisher. The message-driven publish-subscribe implementation is used by the unicast transports. These transports are limited to unicast (point-to-point) communication and have to simulate multicast delivery via a series of point-to-point communications.
The subscribe workflow for unicast transports is as follows
- Subscribers request to a publisher the intent to subscribe to certain message types.
- Publisher stores both the subscriber names and the message types in the persistence.
The publisher's address is provided via routing configuration.
Message-driven publish-subscribe relies on the publisher having access to a persistent store for maintaining the mapping between message types and their subscribers.
Available subscription persisters include
The publish workflow for unicast transports is as follows:
- Some code (e.g. a saga or a handler) requests that a message be published.
- Publisher queries the storage for a list of subscribers.
- Publisher loops through the list and sends a copy of that message to each subscriber.
By default, NServiceBus requires a subscription persister to be configured. If an endpoint does not publish any events, the publish functionality can be disabled to avoid providing a subscription persister:
var transportConfiguration = endpointConfiguration.UseTransport<TransportDefinition>(); transportConfiguration.DisablePublishing();
For multicast transports that support publish–subscribe natively neither persistence nor control message exchange is required to complete the publish-subscribe workflow.
The subscribe workflow for multicast transports is as follows
- Subscribers send request to the broker with the intent to subscribe to certain message types.
- Broker stores the subscription information.
Note that in this case the publisher does not interact in the subscribe workflow.
The publish workflow for multicast transports is as follows
- Some code (e.g. a saga or a handler) request that a message be published.
- Publisher sends the message to the Broker.
- Broker sends a copy of that message to each subscriber.