.NET Development

Real-time Chat using MassTransit, RabbitMQ, and SignalR

I ran across an interesting article where the author had an example chat application with the goal of using RabbitMQ for backend messages and the SignalR for the front-end updates.

The full article is located here: SignalRChat with MassTransit v3

My abbreviated steps and notes:

I have used RabbitMQ before, but only as a stand alone docker container. This article has it being installed on the dev machine. So I went ahead and did that using Chocolatey.

choco install erlang
choco install rabbitmq

I also turned on the RabbitMQ web interface and made the configuration changes as prescribed.

Through the RabbitMQ web interface, I was able to: Create a new user, create a new virtual host, and setup all the permissions.


It was at this moment that I realized that the article was from 2015 and enough breaking changes to MassTransit, Autofac, and other libraries had occurred that I was not going to be able to complete this tutorial.


Not to be cut short, I searched the interwebs for a similar tutorial that is more recent. I found that MassTransit itself has a sample application showing integration with the SignalR backplane.

You can find the description to the project here: https://masstransit-project.com/advanced/signalr/sample.html

The code is actually ready out-of-the-box (or repo) by checking it out of Github here: MassTransit/Sample-SignalR

Basically run the Docker Compose script, get RabbitMQ up and running, and then dotnet run the two websites and console app. I actually ran each running website under two browsers (Chrome and Firefox) just to demonstrate a more real-world app this could be. See below that as a message from one of the three clients submits the message, it appears near-instantly on all websites (the console app doesn’t receive messages, only sends.)

So lets look at the important parts of the code that make this work. One thing to note is that MassTransit made this a super simple website with no bells-and-whistles so to not get those confused with the demo itself. It appears there are a couple of other chat demos linked that are fancier.

The dependency injection service setup is pretty straight forward.

  1. Add SignalR
  2. Add MassTransit
    1. Register the SignalR ChatHub class using the SignalR integration.
    2. Register RabbitMQ as the messaging queue
  3. Add MassTransit Hosted Service

The configure method does the typical setup with the added health check setup and the SignalR ChatHub endpoint setup.

The ChatHub has a single method called Send that takes a name and a message.

The website script is just straight-up JavaScript that registers the page to the server as an observer, and it then listens for SignalR messages and once a message is received it posts that message to the page.

Tracking a message from sender to receiver(s)

We startup a web instance.

Once our web application is up-and-running, we can see that it is now registered under the RabbitMQ exchange ready to receive messages.

So what is going on here. If we take a look at the source code behind the x.AddSignalRHub<ChatHub>(); that registers SignalR with MassTransit, we see the following:

This shows that the web application is being setup as a consumer to the All, Connection, Group, GroupManagement, and User exchanges inside of RabbitMQ.

Each Hub (i.e. ChatHub) has 5 types:

Overall this tutorial is a great way to get started with MassTransit, SignalR, and RabbitMQ.

There are currently no comments.