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.
BUT ALL IS NOT LOST!
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.
- Add SignalR
- Add MassTransit
- Register the SignalR ChatHub class using the SignalR integration.
- Register RabbitMQ as the messaging queue
- Add MassTransit Hosted Service
The configure method does the typical setup with the added health check setup and the SignalR
ChatHub endpoint setup.
ChatHub has a single method called
Send that takes a name and a message.
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.