0

I had a problem sending messages to clients via MassTransit and SignalR

Startup:

        //SignalR
        services.AddSignalR().AddMassTransitBackplane();     
        #region MassTransit RabbitMq

        services.AddScoped<SendCosistListToScaleConsumer>();
        services.AddScoped<CreateConsistListConsumer>();

        services.AddMassTransit(x => 
        {
            x.AddSignalRHubConsumers<NotifyHub>();

            x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(conf =>
            {
                conf.Host(Configuration["Rabbit:Host"], host => {
                    host.Username(Configuration["Rabbit:Username"]);
                    host.Password(Configuration["Rabbit:Password"]);
                });                   

               conf.ReceiveEndpoint(Configuration["Rabbit:ReceiveEndpoint"], e => {
                    e.PrefetchCount = 16;
                    e.UseMessageRetry(n => n.Interval(3, 100));

                    #region Consumers
                    e.Consumer<SendCosistListToScaleConsumer>();
                    e.Consumer<CreateConsistListConsumer>();
                   #endregion
               });
                conf.AddSignalRHubEndpoints<NotifyHub>(provider);
            }));
        });
        services.AddMassTransitHostedService();
        #endregion

....

app.UseSignalR(endpoints =>
{
    endpoints.MapHub<NotifyHub>("/notify");
});

Consumer:

public class CreateConsistListConsumer : IConsumer<ICreateConsistList>
    {
        IReadOnlyList<IHubProtocol> protocols = new IHubProtocol[] { new JsonHubProtocol() };
        public Task Consume(ConsumeContext<ICreateConsistList> context)
        {
            context.Publish<All<NotifyHub>>(
               new
               {
                   Message = protocols.ToProtocolDictionary("SendMessageToAllUsers", new object[] { "CompanyId", context.Message.CompanyId })
               });
            return Task.CompletedTask;
        }
    }

Console App (SignalR Client):

   hubConnection.On<Object>("SendMessageToAllUsers", param => {
        Console.WriteLine(param);
     });

If I understand correctly how MassTransii and SignalR work, then this code is enough to send messages to clients. With the help of debugging, I looked that CreateConsistListConsumer is working, but clients do not receive reporting.   At the same time, the client connects to the hub and correctly receives messages from other sources, but not from MassTransit.

What am I doing wrong?

  • Did you start the bus? You must call `IBusControl.StartAsync()` before anything will happen! Common mistake. – nizmow Apr 29 '20 at 07:21
  • @nizmow yes, you can see the MassTransit Hosted Service is configured. – Chris Patterson Apr 29 '20 at 12:56
  • Also, the [sample](https://github.com/MassTransit/Sample-SignalR) is a working reference of SignalR and MassTransit. – Chris Patterson Apr 29 '20 at 13:24
  • I configured masstranzit according to the documentation.Here is an example project [link](https://github.com/avrezvanov/MassTransitSignalR.git) – Alexander Rezvanov Apr 30 '20 at 04:11
  • The usage scenario is shown here: [link](https://github.com/avrezvanov/MassTransitSignalR/blob/master/Scenario.png) – Alexander Rezvanov Apr 30 '20 at 04:18
  • I've updated the [sample](https://github.com/MassTransit/Sample-SignalR) to also have a ConsoleClient sample in there. I tested and it works, to receive messages. The short story is you need to connect the client to one of the hubs which would be either http://localhost:5100 or http://localhost:5200. – maldworth May 01 '20 at 01:50
  • @maldworth, Thanks for the console application example. Your example works. But I have a different situation. Service A sends an event, service B catches this event and must send it to the console. But for some reason, the message does not reach the console. I do not understand why. Could you look at my example and point out the error. [sample](https://github.com/avrezvanov/MassTransitSignalR.git) – Alexander Rezvanov May 06 '20 at 05:15

1 Answers1

0

I have been facing the same issue last week.

It seem SignalR is doing some special work with handling hubs, and couldn't make Masstransit SignalR service to work.

I ended up using a static hub reference as described here.

Basically, I am just calling Core DI to get my hub context, then store it into a static property (as in the sample in the Github issue listed above).

When needed, I call the reference from within my MassTransit Consumer, and I am done.

Benjamin Soulier
  • 2,223
  • 1
  • 18
  • 30
  • I am doing the same now. I would like to do it all the same through MassTransit, but it does not seem to work. I hope the developers of MassTransit will pay attention to this. – Alexander Rezvanov May 12 '20 at 02:48
  • Feel free to join the Masstransit Discord channel, Chris is pretty much helping everyone over there https://masstransit-project.com/discord.html – Benjamin Soulier May 14 '20 at 00:21