Search code examples
message-queuemasstransit

MassTransit: how to send a message to specific consumers?


Sorry if my question is dumb, I'm new to MassTransit.

My system consists of a server and multiple client devices. I'd like to send a message from the server to a specific client or to a group of clients. As far as I understand, IBusControl.Publish sends the message to all subscribers, and IBusControl.Send to the only one subscriber.

How can I achieve this using MassTransit? My transports are RabbitMQ / Azure Service Bus.

Thanks!


Solution

  • MassTransit implements standard messaging patterns, which aren't MassTransit-specific. Point-to-point, publish-subscribe, invalid message channel, dead letter channel and so on:

    enter image description here

    You indeed have the choice between sending a message to one consumer using Send and to broadcast messages to all subscribers for that message type by using Publish.

    Everything else can be easily done by adding code to consumers:

    await bus.Publish(new MyMessage { ReceiverGroup = "group1", ... });
    

    and

    public async Task Consume(IContext<MyMessage> context)
    {
        if (context.Message.ReceiverGroup != myGroup) return;
    
        ...
    }