Getting Started
Architecture
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Publish-Subscribe

Component: NServiceBus
NuGet Package: NServiceBus (9.1)

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.

Mechanics

Depending on the features provided by a given transport there are two possible implementations of Publish-Subscribe mechanics: message-driven (persistence-based) and native.

All subscribers gets their own copy of the event

To ensure that each subscriber can process, and potentially retry, the event independent of other subscribers, NServiceBus makes sure that each subscriber gets a copy of the published event delivered to their input queue.

Message-driven (persistence-based)

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.

Subscribe

The subscribe workflow for unicast transports is as follows

  1. Subscribers request to a publisher the intent to subscribe to certain message types.
  2. Publisher stores both the subscriber names and the message types in the persistence.
sequenceDiagram Participant Subscriber1 As Subscriber1 Participant Subscriber2 As Subscriber2 Subscriber1 ->> Publisher: Subscribe to Message1 Publisher ->> Persistence: Store "Subscriber1 wants Message1" Subscriber2 ->> Publisher: Subscribe to Message1 Publisher ->> Persistence: Store "Subscriber2 wants Message1"

The publisher's address is provided via routing configuration.

Publish

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:

  1. Some code (e.g. a saga or a handler) requests that a message be published.
  2. Publisher queries the storage for a list of subscribers.
  3. Publisher loops through the list and sends a copy of that message to each subscriber.
sequenceDiagram Participant Subscriber1 As Subscriber1 Participant Subscriber2 As Subscriber2 Note over Publisher: Publish Message1 occurs Publisher ->> Persistence: Requests "who wants Message1" Persistence ->> Publisher: Subscriber1 and Subscriber2 Publisher ->> Subscriber1: Send Message1 Publisher ->> Subscriber2: Send Message1
Disabling publishing

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(new TransportDefinition());
transportConfiguration.DisablePublishing();

Native

For multicast transports that support publish–subscribe natively neither persistence nor control message exchange is required to complete the publish-subscribe workflow.

Subscribe

The subscribe workflow for multicast transports is as follows

  1. Subscribers send request to the broker with the intent to subscribe to certain message types.
  2. Broker stores the subscription information.

Note that in this case the publisher does not interact in the subscribe workflow.

sequenceDiagram Participant Subscriber1 As Subscriber1 Participant Subscriber2 As Subscriber2 Participant Broker As Broker Participant Publisher As Publisher Subscriber1 ->> Broker: Subscribe to Message1 Subscriber2 ->> Broker: Subscribe to Message1

Publish

The publish workflow for multicast transports is as follows

  1. Some code (e.g. a saga or a handler) request that a message be published.
  2. Publisher sends the message to the Broker.
  3. Broker sends a copy of that message to each subscriber.
sequenceDiagram Participant Subscriber1 As Subscriber1 Participant Subscriber2 As Subscriber2 Participant Transport As Transport Note over Publisher: Publish Message1 occurs Publisher ->> Transport: Sends Message1 Transport ->> Subscriber1: Send Message1 Transport ->> Subscriber2: Send Message1

Samples

Related Articles