Real-time Kafka Message Delivery to Frontend Using SignalR
In this guide, we will demonstrate how to set up a Kafka.SingalR in your application and deliver Kafka messages directly to your frontend client using SignalR. This approach enables real-time updates and seamless communication between your backend and frontend.
- Connect Kafka as a Consumer: Establish a connection to Kafka and consume messages from one or more topics.
- Deliver Kafka Messages Directly to UI Client: Use SignalR to push Kafka messages to the frontend in real-time.
- Support for Multiple Kafka Topics: Easily connect and consume messages from multiple Kafka topics.
- Install the
Kafka.SignalRNuGet package.
- .NET CLI
dotnet add package Kafka.SignalR --version 1.0.0- PackageManager
Install-Package Kafka.SignalR -Version 1.0.0- In
ConfigureServicesinStartup.cs, configure with.AddKafkaSignalR(Configuration.GetSection("Kafka")): - In
ConfigureinStartup.cs, configure withendpoints.MapHub<KafkaSignalRHub>(Configuration["Kafka:Hub"])
Simple configuration:
services.AddKafkaSignalR(Configuration.GetSection("Kafka")) app.UseEndpoints(endpoints =>
{
endpoints
.MapHub<KafkaSignalRHub>(Configuration["Kafka:Hub"])
endpoints
.MapGet("/", async context => {
await context.Response.WriteAsync("Running Kafka Service....");
});
});Simple appsettings.json
"Kafka": {
"Hub": "kafka/hubService",
"Topics": "MyTopic",
"GroupId": "myFirstApp",
"BootstrapServers": "localhost:9092",
"SecurityProtocol": "PlainText", //Ssl or PlainText
"EnableAutoCommit": "true",
"AutoCommitIntervalMs": "600000",
//If Ssl than configure below
"SslCaLocation": "",
"SslCertificateLocation": "",
"SslKeyLocation": ""
},const connection = new signalR.HubConnectionBuilder().withUrl("https://localhost:44363/kafka/hubService").build();
connection.on("RelayMessage", function (topicName,message) {
// your code
});
connection.start().then(function () {
//subscribe to kafka topics
connection.invoke("Subscribe", "MyTopic");
connection.invoke("Subscribe", "MyTopic1");
}).catch(function (err) {
return console.error(err.toString());
});If user want to override message processing before delivering it to the SignalR Client. Please follow the below approach.
public class KafkaMessageProcessService: IKafkaMessageProcessService
{
public string ProcessMessage(string message)
{
string processedMessage = message;
//Do your process
return processedMessage;
}
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IKafkaMessageProcessService, KafkaMessageProcessService>();
...
}Move the demo folder outside of KafkaSignalR folder.
Open demo\UI.ServiceKafkaConsumerService.csproj project