- Home
- .NET tutorials
- How to use SignalR when receiving a message from a RabbitMQ queue in .NET Core
How to use SignalR when receiving a message from a RabbitMQ queue in .NET Core
Published: Wednesday 28 August 2019
The Assumptions
The SignalR Hub
// ChatHub.cs
using Microsoft.AspNetCore.SignalR;
namespace SignalRRabbitMQ_Example.Hubs
{
public class ChatHub : Hub
{
}
}
You then have to configure that Hub in your .NET Core application. This is an example of it in .NET Core 3:
app.UseEndpoints(configure =>
{
configure.MapHub<ChatHub>("/chatHub");
});
In earlier versions of .NET Core, you will use app.useSignalR syntax, but this has been depreciated as of version 3.
Consuming a RabbitMQ on Startup
// IRabbitMQService.cs
public interface IRabbitMQService
{
void Connect();
}
// RabbitMQService.cs
public class RabbitMQService : IRabbitMQService
{
protected readonly ConnectionFactory _factory;
protected readonly IConnection _connection;
protected readonly IModel _channel;
protected readonly IServiceProvider _serviceProvider;
public RabbitMQService(IServiceProvider serviceProvider)
{
// Opens the connections to RabbitMQ
_factory = new ConnectionFactory() { HostName = "localhost" };
_connection = _factory.CreateConnection();
_channel = _connection.CreateModel();
_serviceProvider = serviceProvider;
}
public virtual void Connect()
{
// Declare a RabbitMQ Queue
_channel.QueueDeclare(queue: "TestQueue", durable: true, exclusive: false, autoDelete: false);
var consumer = new EventingBasicConsumer(_channel);
// When we receive a message from SignalR
consumer.Received += delegate (object model, BasicDeliverEventArgs ea) {
// Get the ChatHub from SignalR (using DI)
var chatHub = (IHubContext<ChatHub>)_serviceProvider.GetService(typeof(IHubContext<ChatHub>));
// Send message to all users in SignalR
chatHub.Clients.All.SendAsync("messageReceived", "You have received a message");
};
// Consume a RabbitMQ Queue
_channel.BasicConsume(queue: "TestQueue", autoAck: true, consumer: consumer);
}
}
// Startup.cs
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using SignalRRabbitMQ_Example.Hubs;
namespace SignalRRabbitMQ_Example
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IRabbitMQService, RabbitMQService>(); // Need a single instance so we can keep the referenced connect with RabbitMQ open
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostApplicationLifetime lifetime)
{
// Prior to .NET Core 3, IHostApplicationLifetime was actually IApplicationLifetime
// Add the ChatHub to the configuration. This has changed for .NET Core 3. Prior versions used app.AddSignalR at this stage
app.UseEndpoints(configure =>
{
configure.MapHub<ChatHub>("/chatHub");
});
// Run 'RegisterSignalRWithRabbitMQ' when the application has started.
lifetime.ApplicationStarted.Register(() => RegisterSignalRWithRabbitMQ(app.ApplicationServices));
}
public void RegisterSignalRWithRabbitMQ(IServiceProvider serviceProvider)
{
// Connect to RabbitMQ
var rabbitMQService = (IRabbitMQService)serviceProvider.GetService(typeof(IRabbitMQService));
rabbitMQService.Connect();
}
}
}
Give us your anonymous feedback regarding this page, or any other areas of the website.
Watch .NET tutorials
Subscribe to our YouTube channel
Related tutorials
Add a SignalR hub to ASP.NET Core & connect using JavaScript
Learn how to add a SignalR hub to an ASP.NET Core app and how to connect to it by using a WebSocket through JavaScript.