Skip to content

Broadcast Messages to a Specific Topic

If you're using a transport endpoint that supports publishing messages by topic such as this example using Rabbit MQ from the Wolverine tests:

cs
theSender = Host.CreateDefaultBuilder()
    .UseWolverine(opts =>
    {
        opts.UseRabbitMq("host=localhost;port=5672").AutoProvision();
        opts.PublishAllMessages().ToRabbitTopics("wolverine.topics", exchange =>
        {
            exchange.BindTopic("color.green").ToQueue("green");
            exchange.BindTopic("color.blue").ToQueue("blue");
            exchange.BindTopic("color.*").ToQueue("all");
        });

        opts.PublishMessagesToRabbitMqExchange<RoutedMessage>("wolverine.topics", m => m.TopicName);
    }).Start();

snippet source | anchor

You can explicitly publish a message to a topic through this syntax:

cs
var publisher = theSender.Services
    .GetRequiredService<IMessageBus>();

await publisher.BroadcastToTopicAsync("color.purple", new Message1());

snippet source | anchor

cs
var publisher = theSender.Services
    .GetRequiredService<IMessageBus>();

await publisher.BroadcastToTopicAsync("color.purple", new Message1());

snippet source | anchor

WARNING

If you wish to use this functionality, you have to configure at least one sending endpoint subscription like a Rabbit MQ topic exchange in your application. Wolverine has to know how to send messages with your topic.

Topic Sending as Cascading Message

Wolverine is pretty serious about enabling as many message handlers or HTTP endpoints as possible to be pure functions where the unit testing is easier, so there's an option to broadcast messages to a particular topic as a cascaded message:

cs
public class ManuallyRoutedTopicResponseHandler
{
    public IEnumerable<object> Consume(MyMessage message, Envelope envelope)
    {
        // Go North now at the "direction" queue
        yield return new GoNorth().ToTopic($"direction/{envelope.TenantId}");
    }
}

snippet source | anchor

Released under the MIT License.