FanOut (multiplying messages)

FanOut component

FanOut is a component that receives messages from a topic and passes them to all subscribers. In effect, messages are “multiplied”.

A typical use case for using FanOut is having one external subscription and multiple workers inside the process.

Configuring

// ...
// NewFanOut creates a new FanOut.
func NewFanOut(
	subscriber message.Subscriber,
	logger watermill.LoggerAdapter,
) (*FanOut, error) {
// ...

Full source: github.com/ThreeDotsLabs/watermill/pubsub/gochannel/fanout.go

You need to call AddSubscription method for all topics that you want to listen to. This needs to be done before starting the FanOut.

// ...
// AddSubscription add an internal subscription for the given topic.
// You need to call this method with all topics that you want to listen to, before the FanOut is started.
// AddSubscription is idempotent.
func (f *FanOut) AddSubscription(topic string) {
// ...

Full source: github.com/ThreeDotsLabs/watermill/pubsub/gochannel/fanout.go

Running

// ...
// Run runs the FanOut.
func (f *FanOut) Run(ctx context.Context) error {
// ...

Full source: github.com/ThreeDotsLabs/watermill/pubsub/gochannel/fanout.go

Then, use it as any other message.Subscriber.


Check our online hands-on training