Friday, August 14, 2026

SignalR vs Azure Service Bus vs Kafka vs RabbitMQ: Complete Guide with Real-Time Examples and C# Code

 

SignalR vs Azure Service Bus vs Kafka vs RabbitMQ: Complete Guide with Real-Time Examples and C# Code

Modern enterprise applications often use multiple applications, microservices, background workers, and front-end clients that need to communicate with each other. Choosing the right communication technology is therefore an important architectural decision.

Four technologies frequently considered in .NET and microservices architectures are:

  • ASP.NET Core SignalR

  • Azure Service Bus

  • RabbitMQ

  • Apache Kafka

Although they are sometimes compared with each other, they are designed for different communication patterns.

The most important principle is:

SignalR is primarily for real-time client communication, Azure Service Bus and RabbitMQ are message brokers, while Kafka is primarily an event-streaming platform.


1. Introduction

Consider an enterprise e-commerce application:

                    Angular Application
                           |
                           v
                     Order Web API
                           |
             +-------------+-------------+
             |             |             |
             v             v             v
        Payment        Inventory     Notification
        Service          Service        Service

Now consider the following requirements:

  1. Notify the customer's browser immediately when the order status changes.

  2. Send an order-processing message reliably to another microservice.

  3. Route messages to different queues based on business requirements.

  4. Store large volumes of events and allow multiple applications to process them independently.

  5. Replay historical events for analytics or recovery.

One technology is not necessarily the best solution for all these requirements.

This is where SignalR, Azure Service Bus, RabbitMQ and Kafka come into the picture.


2. Quick Comparison

TechnologyPrimary PurposeBest Use Case
SignalRReal-time client communicationNotifications, chat, live dashboards
Azure Service BusEnterprise messagingReliable microservice communication
RabbitMQMessage brokerQueues, routing, work distribution
KafkaEvent streamingHigh-volume events, analytics, event pipelines

3. SignalR

SignalR is an ASP.NET Core library designed for real-time communication between a server and connected clients.

Instead of the client repeatedly asking:

Is my order ready?

Is my order ready?

Is my order ready?

the server can push an update immediately:

Order #1001 has been shipped.

SignalR supports WebSockets and fallback transports such as Server-Sent Events and Long Polling.

Typical SignalR use cases

  • Real-time notifications

  • Chat applications

  • Live dashboards

  • Order tracking

  • Stock/price updates

  • Monitoring applications

  • Progress notifications

  • Collaborative applications


4. SignalR Architecture

                ASP.NET Core Server
                       |
                       |
                    SignalR
                       |
              WebSocket Connection
                       |
          +------------+------------+
          |            |            |
          v            v            v
       Browser 1    Browser 2    Browser 3

The important point is that SignalR is generally about connected clients.

It is not intended to replace a durable enterprise message broker.


5. SignalR C# Example

Install the SignalR package if required:

dotnet add package Microsoft.AspNetCore.SignalR

Create a Hub:

public class NotificationHub : Hub
{
    public async Task SendNotification(string message)
    {
        await Clients.All.SendAsync(
            "ReceiveNotification",
            message);
    }
}

Configure the Hub:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSignalR();

var app = builder.Build();

app.MapHub<NotificationHub>("/notificationHub");

app.Run();

A client can connect to:

/notificationHub

The server can then send:

await hubContext.Clients.All.SendAsync(
    "ReceiveNotification",
    "Order #1001 has been shipped.");

All connected clients receive the notification.


6. SignalR with Angular

Install the SignalR client:

npm install @microsoft/signalr

Create a connection:

import * as signalR from '@microsoft/signalr';

const connection =
    new signalR.HubConnectionBuilder()
        .withUrl('https://localhost:7000/notificationHub')
        .build();

connection.on('ReceiveNotification', message => {
    console.log(message);
});

await connection.start();

The communication looks like:

ASP.NET Core
     |
     | SignalR
     | WebSocket
     v
Angular Application

7. Azure Service Bus

Azure Service Bus is a fully managed enterprise message broker.

It is designed for reliable communication between applications and services.

Microsoft describes Azure Service Bus as a cloud messaging system supporting queues, topics, subscriptions and enterprise messaging scenarios.

Consider:

Order API
    |
    | OrderCreated
    v
Azure Service Bus
    |
    +------> Payment Service
    |
    +------> Inventory Service
    |
    +------> Notification Service

The producer and consumer don't need to execute at exactly the same time.

The broker provides the decoupling layer.


8. Azure Service Bus Queue

A queue generally represents a point-to-point communication model.

Producer
   |
   v
+----------------+
| Order Queue    |
+----------------+
   |
   v
Consumer

For example:

Order API
    |
    v
OrderQueue
    |
    v
Payment Service

The message can remain available until a consumer successfully processes it.

This is particularly useful for background processing and microservice workflows.


9. Azure Service Bus C# Producer

Install:

dotnet add package Azure.Messaging.ServiceBus

Producer:

using Azure.Messaging.ServiceBus;
using System.Text.Json;

string connectionString = "...";
string queueName = "orders";

await using var client =
    new ServiceBusClient(connectionString);

ServiceBusSender sender =
    client.CreateSender(queueName);

var order = new
{
    OrderId = 1001,
    CustomerId = 5001,
    Amount = 2500
};

string json = JsonSerializer.Serialize(order);

var message = new ServiceBusMessage(json);

await sender.SendMessageAsync(message);

Architecture:

Order API
    |
    | SendMessageAsync()
    v
Azure Service Bus
    |
    v
Order Queue

10. Azure Service Bus Consumer

ServiceBusProcessor processor =
    client.CreateProcessor(queueName);

processor.ProcessMessageAsync += async args =>
{
    string message =
        args.Message.Body.ToString();

    Console.WriteLine(message);

    await args.CompleteMessageAsync(args.Message);
};

processor.ProcessErrorAsync += args =>
{
    Console.WriteLine(args.Exception);
    return Task.CompletedTask;
};

await processor.StartProcessingAsync();

The consumer processes the message and explicitly completes it.


11. Azure Service Bus Topic

A topic is useful when the same business event needs to reach multiple subscribers.

                    Order API
                       |
                       v
                 OrderCreated
                       |
                       v
                Azure Service Bus
                     Topic
                       |
          +------------+------------+
          |            |            |
          v            v            v
     Subscription  Subscription  Subscription
       Payment       Inventory    Notification

For example:

OrderCreated
      |
      +---- Payment Service
      |
      +---- Inventory Service
      |
      +---- Notification Service
      |
      +---- Audit Service

Azure Service Bus topics and subscriptions provide a publish/subscribe model.


12. RabbitMQ

RabbitMQ is a popular message broker.

One of its most important architectural concepts is the exchange.

The typical flow is:

Producer
   |
   v
Exchange
   |
   | Routing
   |
   +--------+--------+
   |        |        |
   v        v        v
Queue A   Queue B   Queue C
   |        |        |
   v        v        v
Consumer  Consumer  Consumer

RabbitMQ supports different exchange types, including:

  • Direct

  • Topic

  • Fanout

  • Headers

This provides flexible message-routing capabilities.


13. RabbitMQ C# Producer

A commonly used .NET package is:

dotnet add package RabbitMQ.Client

Example:

using RabbitMQ.Client;
using System.Text;

var factory = new ConnectionFactory
{
    HostName = "localhost"
};

using var connection =
    await factory.CreateConnectionAsync();

using var channel =
    await connection.CreateChannelAsync();

await channel.QueueDeclareAsync(
    queue: "orders",
    durable: true,
    exclusive: false,
    autoDelete: false);

string message = "Order #1001 created";

byte[] body =
    Encoding.UTF8.GetBytes(message);

await channel.BasicPublishAsync(
    exchange: "",
    routingKey: "orders",
    body: body);

The message is sent to the RabbitMQ broker.


14. RabbitMQ Consumer

var consumer =
    new AsyncEventingBasicConsumer(channel);

consumer.ReceivedAsync += async (sender, args) =>
{
    string message =
        Encoding.UTF8.GetString(args.Body.ToArray());

    Console.WriteLine(message);

    await channel.BasicAckAsync(
        args.DeliveryTag,
        multiple: false);
};

await channel.BasicConsumeAsync(
    queue: "orders",
    autoAck: false,
    consumer: consumer);

The acknowledgement tells RabbitMQ that the message has been successfully processed.


15. Kafka

Apache Kafka is primarily an event-streaming platform.

This is an important distinction.

Kafka is not simply:

Producer -> Queue -> Consumer

Instead, Kafka uses:

Producer
    |
    v
Kafka Topic
    |
    +--- Partition 0
    |
    +--- Partition 1
    |
    +--- Partition 2

Kafka topics are divided into partitions, allowing data to be distributed and processed in parallel. Kafka consumers use offsets to track their position in the stream.


16. Kafka Consumer Groups

This is one of Kafka's most important concepts.

Suppose:

OrderCreated

is published to Kafka.

Multiple independent systems can consume it.

                    Kafka Topic
                        |
              +---------+---------+
              |         |         |
              v         v         v
          Payment    Analytics   Fraud
           Group       Group      Group

Each consumer group maintains its own position.

Therefore, the same event can be processed independently by multiple systems.


17. Kafka C# Producer

Install:

dotnet add package Confluent.Kafka

Producer:

using Confluent.Kafka;

var config = new ProducerConfig
{
    BootstrapServers = "localhost:9092"
};

using var producer =
    new ProducerBuilder<string, string>(config)
        .Build();

await producer.ProduceAsync(
    "orders",
    new Message<string, string>
    {
        Key = "1001",
        Value = "Order #1001 created"
    });

The architecture is:

Order API
    |
    v
Kafka Producer
    |
    v
orders topic

18. Kafka Consumer

var config = new ConsumerConfig
{
    BootstrapServers = "localhost:9092",
    GroupId = "payment-service",
    AutoOffsetReset = AutoOffsetReset.Earliest
};

using var consumer =
    new ConsumerBuilder<string, string>(config)
        .Build();

consumer.Subscribe("orders");

while (true)
{
    var result = consumer.Consume();

    Console.WriteLine(
        $"Received: {result.Message.Value}");
}

The consumer group is:

payment-service

Kafka uses offsets to keep track of which events have been processed.


19. The Most Important Difference: Queue vs Event Stream

This is one of the most frequently asked interview questions.

Traditional Message Queue

Conceptually:

Producer
   |
   v
Queue
   |
   v
Consumer

The primary purpose is to deliver work/messages to consumers.

Examples:

Azure Service Bus Queue
RabbitMQ Queue

Kafka Event Stream

Kafka is based around a durable event log:

Producer
   |
   v
+-----------------------------+
| Kafka Topic                 |
|                             |
| E1 E2 E3 E4 E5 E6 E7 ...    |
+-----------------------------+
       |
       +---- Consumer Group A
       |
       +---- Consumer Group B
       |
       +---- Consumer Group C

Events can be retained according to Kafka's retention configuration, and consumers can maintain their own offsets.

This makes Kafka particularly powerful for:

  • Event streaming

  • Event replay

  • Analytics

  • Data pipelines

  • Audit/event history

  • High-volume processing


20. SignalR vs Azure Service Bus

These technologies solve very different problems.

SignalR

Server
   |
   | Real-time
   v
Browser

Azure Service Bus

Service A
   |
   v
Azure Service Bus
   |
   v
Service B
FeatureSignalRAzure Service Bus
Real-time browser updatesExcellentNo
WebSocketsYesNo
Service-to-service messagingNot primaryExcellent
Durable messagingNot primaryYes
QueueNoYes
Topic/SubscriptionClient groupsYes
ChatExcellentNot primary
Background processingNot primaryExcellent
Enterprise workflowsNot primaryExcellent

21. Azure Service Bus vs RabbitMQ

These are much closer competitors.

FeatureAzure Service BusRabbitMQ
Message queueYesYes
Pub/SubYesYes
RoutingGoodExcellent
Managed Azure serviceYesNo, unless using a managed RabbitMQ offering
Self-hostingNoYes
Azure integrationExcellentGood
ExchangesNoYes
Routing keysNoYes
Enterprise messagingExcellentExcellent
Operational overheadLowerHigher if self-managed

If your application is heavily based on Azure, Azure Service Bus is often a natural choice because it is a managed Azure service.

RabbitMQ becomes attractive when you need broker-level control, flexible routing, self-hosting, or a platform-independent messaging layer.


22. RabbitMQ vs Kafka

These are also frequently compared.

FeatureRabbitMQKafka
Primary purposeMessage brokerEvent streaming
QueueExcellentDifferent model
RoutingExcellentGood
Event streamingLimited compared with KafkaExcellent
Event retentionNot its primary modelCore capability
Event replayNot primaryExcellent
Consumer groupsDifferent modelCore capability
PartitioningNo Kafka-style partition modelCore capability
High-volume streamsGoodExcellent
Work queuesExcellentPossible
Complex routingExcellentLess central
Analytics pipelinesGoodExcellent

23. Kafka vs Azure Service Bus

FeatureKafkaAzure Service Bus
Event streamingExcellentGood
Traditional queuesPossibleExcellent
Event replayExcellentDifferent model
Consumer groupsCore featureDifferent model
PartitioningCore featureDifferent scaling model
Enterprise messagingExcellentExcellent
AnalyticsExcellentGood
Data pipelinesExcellentGood
Azure-native applicationsGoodExcellent
Operational complexityHigherLower
Event historyExcellentDifferent model

24. Real-World E-Commerce Architecture

Let's combine these technologies in a realistic enterprise application.

                        Angular
                           |
                           | HTTPS
                           v
                     Order Web API
                           |
                           |
                    +------+------+
                    |             |
                    v             v
              Azure Service      Kafka
                  Bus              |
                    |              |
          +---------+---------+    +----------+
          |         |         |    |          |
          v         v         v    v          v
       Payment   Inventory Notification Analytics
       Service     Service    Service
                                     
                           |
                           v
                       SignalR
                           |
                           v
                     Angular Browser

Let's understand the flow.


25. Step 1 — Customer Places Order

Angular calls:

POST /api/orders

The Order API creates the order.

Angular
   |
   v
Order API
   |
   v
Order Created

26. Step 2 — Azure Service Bus

The Order API sends a business message:

OrderCreated

to Azure Service Bus.

Order API
    |
    v
Azure Service Bus
    |
    +---- Payment Service
    |
    +---- Inventory Service

This gives the services loose coupling.


27. Step 3 — Kafka

The system can also publish an event:

OrderCreated

to Kafka.

                    Kafka
                      |
          +-----------+-----------+
          |           |           |
          v           v           v
      Analytics      Fraud      Reporting

These systems can independently process the event.


28. Step 4 — SignalR

Once the order status changes:

Order Processing
       |
       v
Order Shipped

the backend sends a SignalR notification:

Order Service
      |
      v
SignalR Hub
      |
      v
Angular

The customer immediately sees:

Your order has been shipped!

29. Complete Enterprise Architecture

                         +----------------+
                         |    Angular     |
                         +-------+--------+
                                 |
                                 | REST
                                 v
                         +---------------+
                         |   API Layer   |
                         +-------+-------+
                                 |
                         +-------+-------+
                         |               |
                         v               v
                +----------------+   +----------+
                | Azure Service  |   |  Kafka   |
                |     Bus        |   |          |
                +-------+--------+   +-----+----+
                        |                  |
             +----------+----------+      |
             |          |          |      |
             v          v          v      v
          Payment   Inventory  Notification Analytics
          Service     Service      Service
             |
             |
             v
       Business Processing
             |
             v
         SignalR Hub
             |
             v
          Angular UI

This architecture demonstrates why these technologies should not automatically be treated as interchangeable.


30. When Should You Use SignalR?

Use SignalR when the requirement is:

"I need to push information from my server to connected clients immediately."

Examples:

Live notification
Live chat
Order status
Real-time dashboard
Progress updates
Monitoring
Collaboration

31. When Should You Use Azure Service Bus?

Use Azure Service Bus when the requirement is:

"I need reliable enterprise messaging between applications or microservices."

Examples:

Order processing
Payment processing
Inventory processing
Background jobs
Business workflows
Microservice communication
Enterprise integration

32. When Should You Use RabbitMQ?

Use RabbitMQ when the requirement is:

"I need a flexible message broker with sophisticated routing and queue-based processing."

Examples:

Work queues
Task distribution
Microservice messaging
Complex routing
Pub/Sub
Self-hosted messaging infrastructure

33. When Should You Use Kafka?

Use Kafka when the requirement is:

"I need a high-throughput, durable event stream that can be consumed independently by many applications."

Examples:

IoT events
Clickstream
Analytics
Data pipelines
Event-driven architecture
Audit events
Financial events
Real-time processing
Large-scale event ingestion

34. Can We Use All Four Together?

Yes.

In a large enterprise system, using multiple technologies can be completely valid.

For example:

                 Customer
                    |
                    v
                Angular
                    |
                    v
                ASP.NET
                    |
          +---------+---------+
          |                   |
          v                   v
    Azure Service Bus       Kafka
          |                   |
          v                   v
   Business Services      Analytics
          |
          v
       SignalR
          |
          v
       Angular

RabbitMQ could also be introduced for a specialized workload where its routing or queueing model is advantageous.

The important thing is not to add technologies unnecessarily.


35. Common Architectural Mistakes

Mistake 1: Using SignalR as a message broker

Don't design:

Order API
    |
    v
SignalR
    |
    v
Payment Service

just because SignalR can send messages.

SignalR is primarily intended for real-time client communication.


Mistake 2: Using Kafka for every small background job

Kafka is extremely powerful, but that doesn't mean every background task requires Kafka.

For a simple:

Order API
    |
    v
Process Invoice

a traditional queue such as Azure Service Bus or RabbitMQ may be simpler.


Mistake 3: Choosing Kafka because it is "fast"

Architecture should not be:

Kafka is fast
       ↓
Use Kafka everywhere

Instead ask:

Do I need event streaming?
Do I need retention?
Do I need replay?
Do I need many consumer groups?
Do I need high-volume event processing?

If yes, Kafka becomes a strong candidate.


36. Interview Question: Are SignalR, Kafka, RabbitMQ and Service Bus Alternatives?

Answer: Not completely.

They overlap in some areas, but their primary purposes are different.

SignalR
   ↓
Real-time client communication

Azure Service Bus
   ↓
Enterprise message broker

RabbitMQ
   ↓
Message broker + flexible routing

Kafka
   ↓
Distributed event streaming

37. Interview Question: Kafka vs RabbitMQ?

A good answer:

RabbitMQ is primarily a message broker focused on queues, routing and message delivery, whereas Kafka is primarily an event-streaming platform designed around durable event streams, partitions, consumer groups and high-throughput processing. I would typically consider RabbitMQ for work queues and complex routing, and Kafka when I need scalable event streaming, retention, replay and multiple independent consumers.


38. Interview Question: SignalR vs Kafka?

A good answer:

SignalR is designed for real-time communication between servers and connected clients, typically browsers or mobile applications. Kafka is designed for durable, scalable event streaming between backend systems. In an enterprise application, I might use Kafka to distribute an OrderShipped event internally and SignalR to push the resulting status update to the customer's browser.


39. Interview Question: Service Bus vs RabbitMQ?

A good answer:

Both are message brokers. Azure Service Bus is a managed Azure messaging service with strong integration into the Azure ecosystem, while RabbitMQ provides a flexible broker with exchanges, bindings and routing capabilities and can be self-hosted. If the application is Azure-centric and we want minimal infrastructure management, Service Bus is attractive. If we require greater broker-level control or specific RabbitMQ routing capabilities, RabbitMQ can be a better choice.


40. Interview Question: Can SignalR and Azure Service Bus Be Used Together?

Absolutely.

For example:

Order Service
     |
     | OrderCompleted
     v
Azure Service Bus
     |
     v
Notification Service
     |
     v
SignalR
     |
     v
Browser

The Service Bus provides reliable backend messaging.

SignalR provides real-time delivery to the user.

This is often a cleaner architecture than trying to use SignalR for both responsibilities.


41. Final Decision Matrix

RequirementRecommended Technology
Real-time browser notificationSignalR
ChatSignalR
Live dashboardSignalR
Microservice commandAzure Service Bus / RabbitMQ
Enterprise business messagingAzure Service Bus
Complex broker routingRabbitMQ
Background work queueAzure Service Bus / RabbitMQ
High-volume event streamKafka
Event replayKafka
Data/analytics pipelineKafka
Multiple independent consumersKafka
Azure-native messagingAzure Service Bus
Self-hosted brokerRabbitMQ / Kafka
Server-to-browser communicationSignalR

42. Conclusion

SignalR, Azure Service Bus, RabbitMQ and Kafka are powerful technologies, but they solve different problems.

The easiest way to remember the difference is:

+---------------------------------------------------+
|                   COMMUNICATION                   |
+---------------------------------------------------+
|                                                   |
|  SignalR                                          |
|  Server ---> Connected Clients                   |
|                                                   |
|  Azure Service Bus                                |
|  Application ---> Reliable Business Message      |
|                                                   |
|  RabbitMQ                                         |
|  Producer ---> Broker ---> Routed Queue           |
|                                                   |
|  Kafka                                            |
|  Producer ---> Durable Event Stream ---> Consumers|
|                                                   |
+---------------------------------------------------+

In one sentence:

Use SignalR for real-time client communication, Azure Service Bus for reliable Azure enterprise messaging, RabbitMQ for flexible broker-based messaging and routing, and Kafka for scalable, durable event streaming.

For a modern .NET 9/10 + Angular + Microservices + Azure enterprise application, a combination such as Azure Service Bus + Kafka + SignalR can be very effective when each technology is assigned a clear responsibility.

Thursday, August 13, 2026

ASP.NET Core SignalR: Complete Guide with Real-Time Examples

ASP.NET Core SignalR: Complete Guide with Real-Time Examples

Introduction

In traditional web applications, the client usually sends a request to the server and waits for a response.

For example:

Angular Application
       |
       | HTTP Request
       v
ASP.NET Core Web API
       |
       | HTTP Response
       v
Angular Application

This works very well for normal CRUD operations.

But consider applications where information needs to appear immediately:

  • 💬 Chat applications

  • 🔔 Real-time notifications

  • 📊 Live dashboards

  • 🚚 Order/shipment tracking

  • 🏦 Stock-price updates

  • 🛒 E-commerce order status

  • 🏭 IoT monitoring

  • 🎮 Multiplayer applications

  • 👥 Collaboration applications

  • 📢 System alerts

Polling the server repeatedly is one possible solution, but it is inefficient.

This is where ASP.NET Core SignalR becomes useful.


1. What is SignalR?

SignalR is a real-time communication library for ASP.NET Core applications.

It allows the server to push data to connected clients without requiring the client to continuously request new data.

The communication can look like this:

                 ASP.NET Core Server
                        |
                        |
                 SignalR Hub
                 /     |      \
                /      |       \
               /       |        \
          Client A   Client B   Client C

When something happens on the server, the server can immediately notify connected clients.

For example:

Order Created
     |
     v
ASP.NET Core
     |
     v
SignalR Hub
     |
     +-----------> Customer 1
     |
     +-----------> Customer 2
     |
     +-----------> Admin Dashboard

No client polling is required.


2. Why Do We Need SignalR?

Suppose an Angular application displays an order status.

The order status changes on the server:

Order #1001
Status = Processing

A few seconds later:

Order #1001
Status = Shipped

Without SignalR, the Angular application might repeatedly call:

GET /api/orders/1001

For example:

Every 5 seconds
     |
     +---- GET /api/orders/1001
     |
     +---- GET /api/orders/1001
     |
     +---- GET /api/orders/1001
     |
     +---- GET /api/orders/1001

This is called polling.

It creates unnecessary HTTP requests.

With SignalR:

Server detects status change
           |
           v
      SignalR Hub
           |
           v
     Connected Client
           |
           v
     UI updates immediately

3. SignalR Architecture

A simplified SignalR architecture looks like this:

             Angular / React / JavaScript
                         |
                         |
                 SignalR Connection
                         |
                         v
                  ASP.NET Core
                         |
                         v
                   SignalR Hub
                         |
              +----------+----------+
              |                     |
          Client A               Client B

The major components are:

  1. Client

  2. SignalR Connection

  3. Hub

  4. Hub Methods

  5. Server-to-Client Calls

  6. Client-to-Server Calls

  7. Groups

  8. Connection Management


4. What is a SignalR Hub?

A Hub is the central communication point between clients and the server.

A Hub is similar to a controller in the sense that it exposes methods, but its purpose is real-time communication rather than traditional HTTP request/response processing.

Example:

public class NotificationHub : Hub
{
    public async Task SendNotification(string message)
    {
        await Clients.All.SendAsync("ReceiveNotification", message);
    }
}

Here:

Clients.All

means all connected clients.

And:

SendAsync("ReceiveNotification", message)

invokes a client-side method named:

ReceiveNotification

5. Creating a SignalR Project

Suppose we have an ASP.NET Core Web API application.

Install SignalR if necessary:

dotnet add package Microsoft.AspNetCore.SignalR

Depending on the ASP.NET Core version and project type, SignalR server functionality may already be included through the ASP.NET Core shared framework.


6. Create a SignalR Hub

Create a folder:

Hubs

Then create:

NotificationHub.cs

Code:

using Microsoft.AspNetCore.SignalR;

public class NotificationHub : Hub
{
    public async Task SendNotification(string message)
    {
        await Clients.All.SendAsync(
            "ReceiveNotification",
            message);
    }
}

This is our first SignalR Hub.


7. Configure SignalR in Program.cs

In ASP.NET Core:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

builder.Services.AddSignalR();

var app = builder.Build();

app.UseHttpsRedirection();

app.MapControllers();

app.MapHub<NotificationHub>("/notificationHub");

app.Run();

The important line is:

builder.Services.AddSignalR();

This registers SignalR.

The following line maps the Hub:

app.MapHub<NotificationHub>("/notificationHub");

The client can connect to:

/notificationHub

For example:

https://localhost:7001/notificationHub

8. Understanding SendAsync()

Consider:

await Clients.All.SendAsync(
    "ReceiveNotification",
    message);

There are three important pieces.

Clients.All

Sends the message to every connected client.

ReceiveNotification

This is the client-side method name.

message

This is the data being sent.

Conceptually:

Server

Clients.All.SendAsync(
      "ReceiveNotification",
      "Order Created"
)

              |
              v

Client A -> ReceiveNotification("Order Created")

Client B -> ReceiveNotification("Order Created")

Client C -> ReceiveNotification("Order Created")

9. JavaScript Client

Install the SignalR JavaScript client:

npm install @microsoft/signalr

Then:

import * as signalR from '@microsoft/signalr';

Create a connection:

const connection =
    new signalR.HubConnectionBuilder()
        .withUrl("https://localhost:7001/notificationHub")
        .withAutomaticReconnect()
        .build();

Start the connection:

connection.start()
    .then(() => {
        console.log("SignalR connected");
    })
    .catch(error => {
        console.error(error);
    });

10. Receiving Messages

The server sends:

await Clients.All.SendAsync(
    "ReceiveNotification",
    message);

The JavaScript client listens for:

connection.on(
    "ReceiveNotification",
    (message) => {
        console.log(message);
    });

Now the complete flow is:

ASP.NET Core

Clients.All.SendAsync(
    "ReceiveNotification",
    message
)

            |
            v

JavaScript

connection.on(
    "ReceiveNotification",
    message => {
        console.log(message);
    }
)

The method names must match:

ReceiveNotification

11. Complete Real-Time Notification Example

Let's build a realistic notification system.

Imagine an e-commerce application.

When an order is created:

Customer places order
       |
       v
Order API
       |
       v
Database
       |
       v
SignalR Hub
       |
       v
Customer/Admin Dashboard

The UI immediately displays:

🔔 New Order Created

Order Number: ORD-1001
Status: Processing

12. Notification Hub

using Microsoft.AspNetCore.SignalR;

public class NotificationHub : Hub
{
    public async Task SendNotification(
        string title,
        string message)
    {
        await Clients.All.SendAsync(
            "ReceiveNotification",
            new
            {
                Title = title,
                Message = message,
                CreatedAt = DateTime.UtcNow
            });
    }
}

Now we can send an object rather than just a string.


13. Calling SignalR from an API Controller

Suppose we have:

[ApiController]
[Route("api/orders")]
public class OrdersController : ControllerBase
{
    private readonly IHubContext<NotificationHub> _hubContext;

    public OrdersController(
        IHubContext<NotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }

    [HttpPost]
    public async Task<IActionResult> CreateOrder(
        CreateOrderRequest request)
    {
        // Save order into database

        var orderId = 1001;

        await _hubContext.Clients.All.SendAsync(
            "OrderCreated",
            new
            {
                OrderId = orderId,
                Status = "Processing"
            });

        return Ok(new
        {
            OrderId = orderId
        });
    }
}

This is an important real-world pattern.

We don't need to call a Hub method directly.

Instead, our Web API can inject:

IHubContext<NotificationHub>

and send messages to clients.


14. What is IHubContext?

IHubContext allows application code outside the Hub to communicate with connected clients.

For example:

Controller
Service
Background Worker
Azure Function
Message Consumer
     |
     v
IHubContext
     |
     v
SignalR Clients

Example:

private readonly IHubContext<NotificationHub> _hubContext;

Then:

await _hubContext.Clients.All.SendAsync(
    "ReceiveNotification",
    "New order created");

15. SignalR Client Types

SignalR provides several ways to target clients.

Clients.All

Send to everyone.

await Clients.All.SendAsync(
    "ReceiveNotification",
    message);

Clients.Caller

Send only to the client that made the request.

await Clients.Caller.SendAsync(
    "ReceiveNotification",
    message);

Clients.Others

Send to everyone except the caller.

await Clients.Others.SendAsync(
    "ReceiveNotification",
    message);

Clients.Client

Send to a specific connection.

await Clients.Client(connectionId)
    .SendAsync(
        "ReceiveNotification",
        message);

16. SignalR Groups

Groups are extremely useful in real-world applications.

Imagine an enterprise application with:

Sales Team
Support Team
Managers
Administrators

We don't want every user to receive every notification.

Instead, we can create groups.

SignalR

+----------------------+
| Sales Group          |
| User A               |
| User B               |
+----------------------+

+----------------------+
| Support Group        |
| User C               |
| User D               |
+----------------------+

+----------------------+
| Manager Group        |
| User E               |
| User F               |
+----------------------+

17. Joining a Group

public async Task JoinGroup(string groupName)
{
    await Groups.AddToGroupAsync(
        Context.ConnectionId,
        groupName);
}

A client can call:

JoinGroup("Sales")

Now the client belongs to the Sales group.


18. Sending to a Group

await Clients.Group("Sales")
    .SendAsync(
        "ReceiveNotification",
        "New sales order received");

Only clients in the Sales group receive the message.

This is very useful for:

  • Department notifications

  • Tenant-based applications

  • Project-based collaboration

  • Team dashboards

  • Chat rooms

  • Order-specific updates


19. Real-Time Order Tracking

Let's create a more realistic example.

Suppose an e-commerce order moves through:

Order Created
     |
     v
Processing
     |
     v
Packed
     |
     v
Shipped
     |
     v
Out for Delivery
     |
     v
Delivered

The server can notify the customer whenever the status changes.

Example:

await _hubContext.Clients
    .Group($"order-{orderId}")
    .SendAsync(
        "OrderStatusChanged",
        new
        {
            OrderId = orderId,
            Status = "Shipped"
        });

Only users watching that order receive the update.


20. Joining an Order Group

public async Task JoinOrderGroup(int orderId)
{
    await Groups.AddToGroupAsync(
        Context.ConnectionId,
        $"order-{orderId}");
}

Client:

await connection.invoke(
    "JoinOrderGroup",
    orderId);

Then listen for:

connection.on(
    "OrderStatusChanged",
    (order) => {
        console.log(
            `Order ${order.orderId} status: ${order.status}`
        );
    }
);

21. Angular + SignalR

SignalR works very well with Angular.

Install:

npm install @microsoft/signalr

Create:

signalr.service.ts

Example:

import { Injectable } from '@angular/core';
import * as signalR from '@microsoft/signalr';

@Injectable({
  providedIn: 'root'
})
export class SignalRService {

  private connection!: signalR.HubConnection;

  startConnection(): void {

    this.connection =
      new signalR.HubConnectionBuilder()
        .withUrl(
          'https://localhost:7001/notificationHub'
        )
        .withAutomaticReconnect()
        .build();

    this.connection
      .start()
      .then(() => {
        console.log('SignalR connected');
      })
      .catch(error => {
        console.error(error);
      });
  }

  onNotification(
    callback: (message: any) => void
  ): void {

    this.connection.on(
      'ReceiveNotification',
      callback
    );
  }
}

22. Using SignalR Service in Angular Component

import { Component, OnInit } from '@angular/core';
import { SignalRService } from './signalr.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html'
})
export class DashboardComponent
  implements OnInit {

  notification: any;

  constructor(
    private signalRService: SignalRService
  ) {}

  ngOnInit(): void {

    this.signalRService.startConnection();

    this.signalRService.onNotification(
      (message) => {

        this.notification = message;

        console.log(
          'Notification received:',
          message
        );
      }
    );
  }
}

23. SignalR Connection Lifecycle

A SignalR connection can have different states:

Disconnected
     |
     v
Connecting
     |
     v
Connected
     |
     v
Reconnecting
     |
     +----------+
     |          |
     v          v
 Connected   Disconnected

You can monitor connection events.

connection.onreconnecting(error => {
    console.log('Connection lost. Reconnecting...');
});

connection.onreconnected(connectionId => {
    console.log(
        'Reconnected:',
        connectionId
    );
});

connection.onclose(error => {
    console.log('SignalR connection closed');
});

24. Automatic Reconnection

SignalR supports automatic reconnect.

const connection =
    new signalR.HubConnectionBuilder()
        .withUrl('/notificationHub')
        .withAutomaticReconnect()
        .build();

You can also specify retry intervals:

.withAutomaticReconnect([
    0,
    2000,
    5000,
    10000
])

Meaning:

First retry   -> Immediately
Second retry  -> 2 seconds
Third retry   -> 5 seconds
Fourth retry  -> 10 seconds

25. WebSockets and SignalR

SignalR can use different transports.

The primary transport is:

WebSockets

Other supported transports can include:

Server-Sent Events
Long Polling

Conceptually:

SignalR
   |
   +---- WebSockets
   |
   +---- Server-Sent Events
   |
   +---- Long Polling

SignalR selects an appropriate transport based on the environment and client/server capabilities.

Therefore, developers normally don't need to implement WebSocket management manually.


26. SignalR vs WebSocket

A common interview question is:

"What is the difference between WebSockets and SignalR?"

WebSocket

WebSocket is a communication protocol that provides full-duplex communication between client and server.

SignalR

SignalR is a higher-level framework/library that provides real-time communication and can use WebSockets when available, while also supporting fallback transports.

FeatureWebSocketSignalR
CommunicationReal-timeReal-time
AbstractionLow-levelHigh-level
Automatic reconnectImplement yourselfSupported
GroupsImplement yourselfBuilt-in
Client managementManualBuilt-in abstractions
ASP.NET Core integrationManualExcellent
Hub conceptNoYes

27. SignalR vs Polling

Polling

Client
  |
  | Request
  v
Server
  |
  | Response
  v
Client

Wait

Client
  |
  | Request
  v
Server

This generates repeated requests.

SignalR

Client
   |
   | Establish connection
   v
SignalR Server
   |
   | Connection remains available
   |
   | <--- Real-time message
   |
   | <--- Real-time message
   |
   | <--- Real-time message

This is much more suitable for real-time communication.


28. Authentication with SignalR

Enterprise applications usually need authentication.

For example:

Angular
   |
   | JWT
   v
ASP.NET Core
   |
   v
SignalR Hub

Configure authentication:

builder.Services.AddAuthentication(
    JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        // JWT configuration
    });

Then:

builder.Services.AddAuthorization();

Hub:

[Authorize]
public class NotificationHub : Hub
{
}

Now only authenticated users can connect to the Hub.


29. Accessing the Current User

Inside a Hub:

var userId =
    Context.User?.FindFirst("sub")?.Value;

You can also access:

Context.ConnectionId

and:

Context.User

This is useful for user-specific notifications.


30. User-Specific Notifications

Suppose User 1001 receives an order update.

We can send a message to that specific user:

await _hubContext.Clients
    .User("1001")
    .SendAsync(
        "ReceiveNotification",
        "Your order has been shipped.");

This is different from:

Clients.All

because only the specified user receives the notification.


31. Real-Time Enterprise Architecture

A typical enterprise architecture might look like:

                Angular
                   |
                   |
             API Management
                   |
          +--------+--------+
          |                 |
       Web API          SignalR
          |                 |
          |                 |
     Microservices      SignalR Hub
          |                 |
          |          +------+------+
          |          |             |
          v          v             v
       Database   Client A      Client B
          |
          v
      Message Bus

For larger systems, events can flow through a message broker.

For example:

Order Service
     |
     v
Azure Service Bus / Kafka
     |
     v
Notification Service
     |
     v
SignalR
     |
     +------> Angular Client
     |
     +------> Admin Dashboard

This provides a scalable architecture.


32. SignalR with Background Services

SignalR is not limited to controllers.

A background service can also send notifications.

For example:

public class OrderMonitoringService
    : BackgroundService
{
    private readonly IHubContext<NotificationHub>
        _hubContext;

    public OrderMonitoringService(
        IHubContext<NotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }

    protected override async Task ExecuteAsync(
        CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            await _hubContext.Clients.All.SendAsync(
                "ReceiveNotification",
                "Background process completed",
                stoppingToken);

            await Task.Delay(
                TimeSpan.FromMinutes(1),
                stoppingToken);
        }
    }
}

Register:

builder.Services.AddHostedService<OrderMonitoringService>();

33. SignalR in Microservices

In a microservices architecture, avoid making every microservice directly responsible for client connections.

A better architecture can be:

              Angular
                 |
                 v
           SignalR Service
                 ^
                 |
          Notification Service
                 ^
                 |
        Azure Service Bus / Kafka
                 ^
                 |
       +---------+---------+
       |         |         |
    Order     Payment   Shipping
   Service     Service   Service

For example:

Order Service
     |
     | OrderCreated event
     v
Azure Service Bus
     |
     v
Notification Service
     |
     v
SignalR
     |
     v
Angular

This creates better separation of responsibilities.


34. Scaling SignalR

This is very important for production systems.

Imagine:

100,000 users

connected to your application.

You may have multiple application instances:

                    Load Balancer
                         |
          +--------------+--------------+
          |              |              |
          v              v              v
      Server 1        Server 2        Server 3
          |              |              |
       SignalR        SignalR        SignalR

The problem is that clients can be connected to different servers.

For example:

Client A ---> Server 1

Client B ---> Server 2

If Server 1 sends a message locally, Server 2 may not automatically know about it.

For scaled-out SignalR deployments, you typically need a backplane or managed SignalR service depending on the architecture.


35. Azure SignalR Service

For Azure-based applications, Azure SignalR Service can handle SignalR connection scaling.

Architecture:

              Angular Clients
             /       |       \
            /        |        \
           v         v         v
      Azure SignalR Service
                 |
                 |
                 v
          ASP.NET Core API
                 |
                 v
          Microservices

This can reduce the responsibility of managing large numbers of persistent client connections in your application servers.

For enterprise applications deployed on Azure, Azure SignalR Service is an important option to evaluate.


36. SignalR in AKS

If your application runs in AKS:

                    Azure
                      |
               Application Gateway
                      |
                  Ingress
                      |
          +-----------+-----------+
          |           |           |
          v           v           v
        Pod 1       Pod 2       Pod 3
          |           |           |
       SignalR     SignalR     SignalR

Persistent connections introduce additional considerations when scaling.

For large-scale deployments, Azure SignalR Service can simplify the connection-management layer.


37. Real-World Use Case: Live Dashboard

Imagine an operations dashboard:

+---------------------------------------+
|       REAL-TIME OPERATIONS DASHBOARD  |
+---------------------------------------+
| Orders Today              12,542      |
| Successful Orders         11,934      |
| Failed Orders                108      |
| Pending Orders               500      |
+---------------------------------------+

Recent Events
-----------------------------------------
10:20:12  Order #1001 Created
10:20:15  Order #1002 Created
10:20:18  Order #1003 Shipped
10:20:22  Order #1004 Delivered

Instead of refreshing the browser:

Dashboard
    |
    | SignalR
    v
ASP.NET Core
    |
    v
Real-time events

The dashboard updates automatically.


38. SignalR Error Handling

Always consider connection failures.

Server:

try
{
    await Clients.All.SendAsync(
        "ReceiveNotification",
        message);
}
catch (Exception ex)
{
    // Log exception
}

Client:

connection.start()
    .catch(error => {
        console.error(
            'SignalR connection failed',
            error
        );
    });

Use proper logging rather than silently ignoring errors.


39. Important SignalR Best Practices

1. Don't put business logic inside the Hub

Avoid:

public async Task CreateOrder()
{
    // 500 lines of business logic
}

Prefer:

Hub
 |
 v
Application Service
 |
 v
Domain Service
 |
 v
Repository

2. Use strongly typed contracts

Instead of sending arbitrary objects everywhere, define DTOs.

public class OrderStatusNotification
{
    public int OrderId { get; set; }

    public string Status { get; set; } = string.Empty;
}

Then:

await _hubContext.Clients.All.SendAsync(
    "OrderStatusChanged",
    notification);

3. Use Groups

For large applications, groups are useful for:

Tenant
Department
Project
Order
Chat Room
Team

4. Enable Authentication

Don't expose sensitive real-time data to unauthenticated clients.


5. Handle Reconnection

Use:

.withAutomaticReconnect()

and monitor:

onreconnecting
onreconnected
onclose

6. Don't Send Excessive Data

Instead of:

Send entire database record

send only what the UI needs:

{
    "orderId": 1001,
    "status": "Shipped"
}

40. Complete SignalR Flow

The complete application flow can be represented as:

             Angular Application
                     |
                     |
             SignalR Connection
                     |
                     v
              NotificationHub
                     |
                     ^
                     |
             IHubContext
                     ^
                     |
              Order Service
                     |
                     v
                  Database

With an event-driven architecture:

Angular
   |
   v
SignalR
   ^
   |
Notification Service
   ^
   |
Azure Service Bus / Kafka
   ^
   |
Order Service
   |
   v
Database

41. SignalR Interview Questions

What is SignalR?

SignalR is a real-time communication library for ASP.NET Core applications that allows servers to push messages to connected clients.

What is a Hub?

A Hub is a high-level abstraction for communication between clients and the server.

What is IHubContext?

IHubContext allows application components outside a Hub to send messages to connected SignalR clients.

What is Clients.All?

It sends a message to all connected clients.

What is Clients.Caller?

It sends a message only to the client that initiated the Hub call.

What are SignalR Groups?

Groups allow connected clients to be logically grouped and targeted collectively.

Can SignalR work with Angular?

Yes. Microsoft provides the @microsoft/signalr JavaScript/TypeScript client.

Does SignalR always use WebSockets?

No. SignalR can use WebSockets when available and supports fallback transports.

How do you scale SignalR?

Depending on the deployment, options include a backplane or a managed service such as Azure SignalR Service.

Can SignalR be used with microservices?

Yes. A common pattern is to publish domain/application events through a message broker and have a notification component deliver relevant events to clients through SignalR.


42. SignalR vs REST API

FeatureREST APISignalR
CommunicationRequest/ResponseReal-time
Server PushNoYes
Persistent ConnectionUsually noYes
CRUDExcellentNot primary purpose
NotificationsLimitedExcellent
ChatNot idealExcellent
Live DashboardPolling requiredExcellent
Order TrackingPolling requiredExcellent
WebSocketsNot inherentSupported

SignalR does not replace REST APIs.

A modern application commonly uses both:

REST API
    |
    +---- CRUD operations

SignalR
    |
    +---- Real-time notifications
    +---- Live status
    +---- Events

43. Recommended Enterprise Architecture

For a large .NET + Angular application, a practical architecture could be:

                         Angular
                            |
             +--------------+--------------+
             |                             |
          REST API                      SignalR
             |                             |
             v                             v
       API Management              Azure SignalR
             |                             |
             v                             |
      ASP.NET Core APIs                    |
             |                             |
       +-----+------+                      |
       |            |                      |
       v            v                      |
    Orders       Payments                  |
       |            |                      |
       +-----+------+                      |
             |                             |
             v                             |
       Azure Service Bus ------------------+
                     |
                     v
              Notification Service

This architecture provides:

  • Separation of responsibilities

  • Real-time client communication

  • Event-driven integration

  • Better scalability

  • Better support for microservices

  • Reduced polling

  • Better user experience


44. Conclusion

SignalR is one of the most useful technologies in the ASP.NET Core ecosystem when an application needs real-time communication.

It is especially useful for:

  • 🔔 Notifications

  • 💬 Chat

  • 📊 Live dashboards

  • 🚚 Order tracking

  • 📈 Monitoring

  • 🏭 IoT applications

  • 👥 Collaboration

  • ⚡ Real-time enterprise applications

The key concepts to remember are:

SignalR
   |
   +-- Hub
   |
   +-- Clients
   |
   +-- IHubContext
   |
   +-- Groups
   |
   +-- Connections
   |
   +-- Authentication
   |
   +-- Automatic Reconnection
   |
   +-- Scaling
   |
   +-- Azure SignalR Service

For a simple application:

Angular
   |
   v
SignalR Hub
   |
   v
ASP.NET Core

For an enterprise application:

Angular
   |
   +------ REST API
   |
   +------ SignalR
             |
             v
      Azure SignalR Service
             |
             v
      Notification Service
             |
             v
   Azure Service Bus / Kafka
             |
             v
       Microservices

The biggest advantage of SignalR is that the application doesn't have to continuously ask:

"Has anything changed?"

Instead, the server can tell the client immediately when something important happens.

That makes SignalR a strong choice for building responsive, event-driven, real-time .NET applications.

Don't Copy

Protected by Copyscape Online Plagiarism Checker