Normal HTTP communication usually follows a request → response model.
sequenceDiagram
Client->>Server: Request
Server-->>Client: Response
But some applications need the server to send data to the client immediately, without waiting for another request.
Examples include:
- Chat applications
- Live notifications
- Live dashboards
- Stock price updates
- Online gaming
- Real-time monitoring
This is where WebSocket and SignalR are useful.
1. What Is WebSocket?
WebSocket is a communication protocol that provides a persistent, two-way communication channel between a client and server.
Once the connection is established, both sides can send messages whenever required.
sequenceDiagram
Client->>Server: Establish WebSocket connection
Server-->>Client: Connection established
Client->>Server: Message
Server-->>Client: Message
Server-->>Client: Push update
Client->>Server: Message
2. HTTP vs WebSocket
| HTTP | WebSocket |
|---|---|
| Request-response | Persistent connection |
| Client normally initiates requests | Both sides can send messages |
| Server does not normally push arbitrary updates | Server can push updates immediately |
| Good for normal APIs | Good for real-time communication |
3. WebSocket Connection
WebSocket initially starts with an HTTP-based handshake. After the connection is upgraded, communication continues using the WebSocket protocol.
sequenceDiagram
Client->>Server: HTTP Upgrade Request
Server-->>Client: WebSocket Upgrade
Client-->>Server: WebSocket Messages
Server-->>Client: WebSocket Messages
4. Why WebSocket?
Imagine a live trading dashboard.
With normal polling, the client repeatedly asks:
Any new price?
Any new price?
Any new price?
With WebSocket, the server can push the update when the price changes.
sequenceDiagram
Client->>Server: Connect
Server-->>Client: Connected
Server-->>Client: Price = 100
Server-->>Client: Price = 101
Server-->>Client: Price = 103
Server-->>Client: Price = 102
5. What Is SignalR?
SignalR is an ASP.NET Core real-time communication framework that makes it easier to build applications where the server needs to push updates to connected clients.
SignalR provides a higher-level programming model than working directly with WebSockets.
flowchart LR
Application["ASP.NET Core Application"]
SignalR["SignalR"]
Transport["Real-Time Transport"]
Client["Client"]
Application --> SignalR
SignalR --> Transport
Transport --> Client
6. SignalR and WebSocket Relationship
This is the important interview point:
WebSocket = Communication protocol
SignalR = Real-time framework
SignalR can use WebSockets when available, while also supporting other transports such as Server-Sent Events and Long Polling.
flowchart TD
SignalR["SignalR"]
SignalR --> WebSocket["WebSocket"]
SignalR --> SSE["Server-Sent Events"]
SignalR --> LongPolling["Long Polling"]
7. Why Use SignalR Instead of Raw WebSocket?
With raw WebSocket, you have to manage more of the communication yourself.
SignalR provides higher-level features such as:
- Connection management
- Automatic transport selection
- Server-to-client messaging
- Client-to-server messaging
- Groups
- Connection lifecycle handling
- Reconnection support
8. SignalR Hub
SignalR uses a concept called a Hub.
A Hub provides a high-level way for clients and servers to communicate with each other.
flowchart LR
Client1["Client 1"]
Client2["Client 2"]
Hub["SignalR Hub"]
Server["ASP.NET Core Server"]
Client1 <--> Hub
Client2 <--> Hub
Server <--> Hub
9. SignalR Example in ASP.NET Core
public class NotificationHub : Hub
{
public async Task SendNotification(string message)
{
await Clients.All.SendAsync(
"ReceiveNotification",
message);
}
}
The server can send a notification to all connected clients.
flowchart LR
Server["ASP.NET Core"]
Hub["Notification Hub"]
Client1["Client 1"]
Client2["Client 2"]
Client3["Client 3"]
Server --> Hub
Hub --> Client1
Hub --> Client2
Hub --> Client3
10. SignalR Groups
SignalR allows clients to be organized into groups.
For example, a notification may need to be sent only to users belonging to the Order-123 group.
flowchart LR
Hub["SignalR Hub"]
Hub --> Group1["Order-123 Group"]
Group1 --> Client1["Client 1"]
Group1 --> Client2["Client 2"]
Hub --> Group2["Order-456 Group"]
Group2 --> Client3["Client 3"]
11. Real-Time Notification Example
Suppose an order status changes from Processing to Shipped.
sequenceDiagram
OrderService->>SignalR: Order shipped
SignalR->>Client1: Order status updated
SignalR->>Client2: Order status updated
The clients receive the update immediately instead of repeatedly polling the API.
12. WebSocket vs SignalR
| WebSocket | SignalR |
|---|---|
| Protocol | Framework / abstraction |
| Lower-level | Higher-level |
| You manage more details | Provides connection management |
| WebSocket only | Can use multiple transports |
| More control | More development convenience |
| Works beyond ASP.NET Core | Strong integration with ASP.NET Core |
13. SignalR vs REST API
| REST API | SignalR |
|---|---|
| Request-response | Real-time communication |
| Client requests data | Server can push data |
| Good for CRUD operations | Good for live updates |
| Usually independent requests | Persistent connection |
14. SignalR in a Scaled Application
A major system-design question is what happens when SignalR runs on multiple servers.
flowchart LR
Client1["Client 1"] --> LB["Load Balancer"]
Client2["Client 2"] --> LB
LB --> Server1["SignalR Server 1"]
LB --> Server2["SignalR Server 2"]
Server1 --> Backplane["Shared Messaging / Backplane"]
Server2 --> Backplane
When SignalR is scaled horizontally, a suitable backplane or managed service can help distribute messages between application instances.
15. SignalR and Redis
Redis can be used as a backplane in a scaled SignalR deployment so that messages can be propagated between SignalR servers.
flowchart LR
Client1["Client"] --> Server1["SignalR Server 1"]
Client2["Client"] --> Server2["SignalR Server 2"]
Server1 --> Redis["Redis Backplane"]
Server2 --> Redis
Redis --> Server1
Redis --> Server2
16. When Should You Use WebSocket?
- You need direct low-level bidirectional communication.
- You need fine-grained control over the protocol and messages.
- You are building a custom real-time protocol.
- You do not need SignalR's higher-level abstractions.
17. When Should You Use SignalR?
- ASP.NET Core application needs real-time communication.
- Live notifications are required.
- Chat functionality is required.
- Live dashboards need server push.
- You want connection management and reconnection support.
- You want to avoid implementing raw WebSocket infrastructure yourself.
18. Important Interview Questions
- What is WebSocket?
- How is WebSocket different from HTTP?
- How does a WebSocket connection work?
- What is SignalR?
- Is SignalR the same as WebSocket?
- What transports can SignalR use?
- What is a SignalR Hub?
- What are SignalR groups?
- WebSocket vs SignalR?
- REST vs SignalR?
- How do you scale SignalR horizontally?
- Why might Redis be used with SignalR?
- When would you choose raw WebSocket over SignalR?
Key Takeaways
- WebSocket provides persistent bidirectional communication.
- WebSocket is useful for real-time applications.
- SignalR is a higher-level real-time framework for ASP.NET Core.
- SignalR can use WebSockets and other supported transports.
- SignalR provides concepts such as Hubs and Groups.
- SignalR simplifies connection and messaging management.
- Scaled SignalR deployments may require shared messaging infrastructure.
Conclusion
WebSocket is the underlying real-time communication protocol, while SignalR provides a higher-level programming model for building real-time applications, especially with ASP.NET Core.
WebSocket → Protocol
SignalR → Real-Time Framework
REST → Request / Response API