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.)
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image.png?resize=1024%2C607&ssl=1)
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.
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-1.png?resize=455%2C317&ssl=1)
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
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-2.png?resize=674%2C287&ssl=1)
The configure method does the typical setup with the added health check setup and the SignalR ChatHub
endpoint setup.
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-3.png?resize=493%2C167&ssl=1)
The ChatHub
has a single method called Send
that takes a name and a message.
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-4.png?resize=731%2C804&ssl=1)
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)
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-5.png?resize=1024%2C137&ssl=1)
We startup a web instance.
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-6.png?resize=1024%2C710&ssl=1)
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:
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-7.png?resize=987%2C377&ssl=1)
This shows that the web application is being setup as a consumer to the All, Connection, Group, GroupManagement, and User exchanges inside of RabbitMQ.
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-8.png?resize=1024%2C500&ssl=1)
Each Hub (i.e. ChatHub) has 5 types:
![](https://i0.wp.com/www.treygourley.com/wp-content/uploads/2021/05/image-9.png?resize=810%2C624&ssl=1)
Overall this tutorial is a great way to get started with MassTransit, SignalR, and RabbitMQ.
Comments