I have implemented a .net core web api with masstransit rabbitMQ and signalR and im trying to make signalR backplane work. I suspect the problem is with exchange bindings that masstransit solves for me but i cant figure out what im doing wrong to have this behaviour.
My goal here is to be able to have an endpoint that another app will call (for now im using postman for this) that will publish a message to all the clients (js side) but whenever the message is published and it arrives to the exchange nothing happens since there is no binding.
In rabbitMQ management tools it creates an exchange without any binding to another exchange/queue
My startup:
public void ConfigureServices(IServiceCollection services)
{
Utilities utilities = new Utilities(Configuration);
RabbitMQIdentity rabbitMQIdentity = utilities.GetRabbitMQIdentity();
var username = rabbitMQIdentity.UserName;
var password = rabbitMQIdentity.Password;
var hostName = rabbitMQIdentity.HostName;
var portNumber = rabbitMQIdentity.Port;
services.AddHttpClient();
services.AddControllers();
services.AddSignalR();
services.AddMassTransit(config =>
{
config.AddSignalRHub<NotificationHub>();
config.UsingRabbitMq((ctx, cfg) =>
{
cfg.Host($"amqp://{username}:{password}@{hostName}:{portNumber}");
cfg.ConfigureEndpoints(ctx);
});
});
services.AddMassTransitHostedService();
services.AddSingleton<IHostEnvironment>(hostEnvironment);
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<LogConfigurationUtility, WebLogConfigurationUtility>();
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
{
builder.SetIsOriginAllowed((x) => Configuration["CorsWhiteList"].Split(';').Any(x.Contains))
.WithMethods("GET", "POST")
.AllowAnyHeader()
.AllowCredentials();
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors();
app.UseMiddleware<RequestMiddleware>();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<NotificationHub>("/notificationhub");
});
}
My publish method:
IReadOnlyList<IHubProtocol> protocols = new IHubProtocol[] { new JsonHubProtocol() };
publishEndpoint.Publish<All<NotificationHub>>(new
{
Messages = protocols.ToProtocolDictionary("Notify", new object[] { "backend-process", "oiiiii" })
});
And my client side:
useEffect(() => {
$(function() {
const connection = new HubConnectionBuilder()
.withUrl(hubUrl)
.configureLogging(LogLevel.Trace)
.build();
// Create a function that the hub can call to broadcast messages.
connection.on("Notify", (status) => {
console.log("entrouuuuuu");
setNotification(status);
});
// Start the connection.
async function start() {
try {
await connection.start();
connection.invoke("InitializeClient", orderId);
console.log("SignalR Connected.");
} catch (err) {
setTimeout(start, 5000);
}
}
start();
});
}, []);
I have followed the official MassTransit documentation and tried to figure out what the sample example differs from mine but i cant seem to be able to figure it out (apart from creating and binding the exchanges correctly).
https://masstransit-project.com/advanced/signalr/quickstart.html https://masstransit-project.com/advanced/signalr/sample.html
Could anyone help me please?