---
title: "How SignalR work?"  
description: "How SignalR work?"  
author: "ICSM Computer"  
published: 2025-06-18  
updated: 2025-06-18  
canonical: https://www.mindstick.com/interview/34254/how-signalr-work  
category: "c#"  
tags: ["c#", "signalr"]  
reading_time: 4 minutes  

---

# How SignalR work?

[SignalR](https://www.mindstick.com/articles/1515/introduction-to-signalr) is a real-time communication library in ASP.NET that allows server-side code to push content to connected clients instantly — without the client having to request it (i.e., no polling). It’s ideal for chat apps, notifications, live dashboards, etc.

### How [SignalR Works (Step-by-Step)](https://www.mindstick.com/interview/34149/what-is-signalr-and-how-does-it-work-in-real-time-web-applications)

- **Connection Establishment**:

   - A client (JavaScript, .NET, or other) connects to the SignalR hub endpoint (e.g., `/chatHub`).
   - SignalR automatically negotiates the best available transport method:

      - **WebSockets** (best)
      - **Server-Sent Events** (for browser clients)
      - **Long Polling** (fallback)

- **Hub Communication**:

   - Server defines a **Hub** class that inherits from `Hub`.
   - Clients call server methods (e.g., `SendMessage`) using the SignalR connection.
   - The server can call back to specific or all clients using `Clients.All.SendAsync(...)`, `Clients.Caller`, `Clients.Others`, etc.

- **Real-Time Messaging**:

   - Messages are serialized and sent over the connection instantly.
   - Clients receive the message and execute corresponding JavaScript/.NET methods.

### Key Components

| Component | Description |
| --- | --- |
| **Hub** | Central server-side class for SignalR logic. |
| **Clients** | API to send messages to connected clients. |
| **Connections** | Each client has a unique connection ID. |
| **Groups** | Logical grouping of clients (e.g., for rooms). |

### Example

#### Server-side (C#):

```cs
public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}
```

#### Client-side (JavaScript):

```javascript
const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub")
    .build();

connection.on("ReceiveMessage", function (user, message) {
    console.log(`${user}: ${message}`);
});

await connection.start();
await connection.invoke("SendMessage", "Anna", "Hello world!");
```

### Security

- SignalR uses authentication (e.g., cookies, JWT tokens).
- You can restrict access to methods using `[Authorize]`.

### Use Cases

- Chat applications
- Stock market/live score updates
- Multiplayer games
- Notifications (in-app or push)
- Collaborative tools (like Google Docs)

## Answers

### Answer by ICSM Computer

[SignalR](https://www.mindstick.com/articles/1515/introduction-to-signalr) is a real-time communication library in ASP.NET that allows server-side code to push content to connected clients instantly — without the client having to request it (i.e., no polling). It’s ideal for chat apps, notifications, live dashboards, etc.

### How [SignalR Works (Step-by-Step)](https://www.mindstick.com/interview/34149/what-is-signalr-and-how-does-it-work-in-real-time-web-applications)

- **Connection Establishment**:

   - A client (JavaScript, .NET, or other) connects to the SignalR hub endpoint (e.g., `/chatHub`).
   - SignalR automatically negotiates the best available transport method:

      - **WebSockets** (best)
      - **Server-Sent Events** (for browser clients)
      - **Long Polling** (fallback)

- **Hub Communication**:

   - Server defines a **Hub** class that inherits from `Hub`.
   - Clients call server methods (e.g., `SendMessage`) using the SignalR connection.
   - The server can call back to specific or all clients using `Clients.All.SendAsync(...)`, `Clients.Caller`, `Clients.Others`, etc.

- **Real-Time Messaging**:

   - Messages are serialized and sent over the connection instantly.
   - Clients receive the message and execute corresponding JavaScript/.NET methods.

### Key Components

| Component | Description |
| --- | --- |
| **Hub** | Central server-side class for SignalR logic. |
| **Clients** | API to send messages to connected clients. |
| **Connections** | Each client has a unique connection ID. |
| **Groups** | Logical grouping of clients (e.g., for rooms). |

### Example

#### Server-side (C#):

```cs
public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}
```

#### Client-side (JavaScript):

```javascript
const connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatHub")
    .build();

connection.on("ReceiveMessage", function (user, message) {
    console.log(`${user}: ${message}`);
});

await connection.start();
await connection.invoke("SendMessage", "Anna", "Hello world!");
```

### Security

- SignalR uses authentication (e.g., cookies, JWT tokens).
- You can restrict access to methods using `[Authorize]`.

### Use Cases

- Chat applications
- Stock market/live score updates
- Multiplayer games
- Notifications (in-app or push)
- Collaborative tools (like Google Docs)


---

Original Source: https://www.mindstick.com/interview/34254/how-signalr-work

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
