---
title: "How do you set up a basic SignalR Hub in an ASP.NET Core application?"  
description: "How do you set up a basic SignalR Hub in an ASP.NET Core application?"  
author: "Utpal Vishwas"  
published: 2025-05-21  
updated: 2025-05-21  
canonical: https://www.mindstick.com/interview/34150/how-do-you-set-up-a-basic-signalr-hub-in-an-asp-dot-net-core-application  
category: "c#"  
tags: ["c#", "signalr", ".net core"]  
reading_time: 4 minutes  

---

# How do you set up a basic SignalR Hub in an ASP.NET Core application?

## 1. Create a new ASP.NET Core project

You can start with an empty Web API or Web app project.

## 2. Add the SignalR package

If you don’t already have it, add the SignalR NuGet package:

```cs
dotnet add package Microsoft.AspNetCore.SignalR
```

## 3. Create a SignalR Hub class

A **Hub** is a central class where clients and server communicate.

Create a new class like this:

```cs
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        // Broadcast the message to all connected clients
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}
```

## 4. Configure SignalR in Startup (`Program.cs` for .NET 6+)

In ASP.NET Core 6 or later, `Program.cs` will look something like this:

```cs
var builder = WebApplication.CreateBuilder(args);

// Add SignalR services
builder.Services.AddSignalR();

var app = builder.Build();

// Map your hub endpoint
app.MapHub<ChatHub>("/chatHub");

app.Run();
```

For ASP.NET Core 3.1 or 5, you add SignalR in `Startup.cs`:

```cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<ChatHub>("/chatHub");
    });
}
```

## 5. Client-side setup

You can use JavaScript to connect to the SignalR hub. Include the SignalR client library (from CDN or npm):

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.7/signalr.min.js"></script>
<script>
    const connection = new signalR.HubConnectionBuilder()
        .withUrl("/chatHub")
        .build();

    // Receive message from server
    connection.on("ReceiveMessage", (user, message) => {
        console.log(`${user}: ${message}`);
        // You can update the DOM here to show the message
    });

    connection.start().catch(err => console.error(err.toString()));

    // Example of sending a message to the hub
    function sendMessage() {
        const user = "User1";
        const message = "Hello SignalR!";
        connection.invoke("SendMessage", user, message)
            .catch(err => console.error(err.toString()));
    }
</script>
```

## Summary

| Step | What to do |
| --- | --- |
| 1. Add SignalR package | Add `Microsoft.AspNetCore.SignalR` |
| 2. Create Hub class | Define methods clients call |
| 3. Register SignalR | In `Program.cs` or `Startup.cs` add SignalR and map hub route |
| 4. Client connect | Use SignalR JS client to connect and call hub methods |

## Answers

### Answer by Utpal Vishwas

## 1. Create a new ASP.NET Core project

You can start with an empty Web API or Web app project.

## 2. Add the SignalR package

If you don’t already have it, add the SignalR NuGet package:

```cs
dotnet add package Microsoft.AspNetCore.SignalR
```

## 3. Create a SignalR Hub class

A **Hub** is a central class where clients and server communicate.

Create a new class like this:

```cs
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

public class ChatHub : Hub
{
    public async Task SendMessage(string user, string message)
    {
        // Broadcast the message to all connected clients
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}
```

## 4. Configure SignalR in Startup (`Program.cs` for .NET 6+)

In ASP.NET Core 6 or later, `Program.cs` will look something like this:

```cs
var builder = WebApplication.CreateBuilder(args);

// Add SignalR services
builder.Services.AddSignalR();

var app = builder.Build();

// Map your hub endpoint
app.MapHub<ChatHub>("/chatHub");

app.Run();
```

For ASP.NET Core 3.1 or 5, you add SignalR in `Startup.cs`:

```cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHub<ChatHub>("/chatHub");
    });
}
```

## 5. Client-side setup

You can use JavaScript to connect to the SignalR hub. Include the SignalR client library (from CDN or npm):

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.7/signalr.min.js"></script>
<script>
    const connection = new signalR.HubConnectionBuilder()
        .withUrl("/chatHub")
        .build();

    // Receive message from server
    connection.on("ReceiveMessage", (user, message) => {
        console.log(`${user}: ${message}`);
        // You can update the DOM here to show the message
    });

    connection.start().catch(err => console.error(err.toString()));

    // Example of sending a message to the hub
    function sendMessage() {
        const user = "User1";
        const message = "Hello SignalR!";
        connection.invoke("SendMessage", user, message)
            .catch(err => console.error(err.toString()));
    }
</script>
```

## Summary

| Step | What to do |
| --- | --- |
| 1. Add SignalR package | Add `Microsoft.AspNetCore.SignalR` |
| 2. Create Hub class | Define methods clients call |
| 3. Register SignalR | In `Program.cs` or `Startup.cs` add SignalR and map hub route |
| 4. Client connect | Use SignalR JS client to connect and call hub methods |


---

Original Source: https://www.mindstick.com/interview/34150/how-do-you-set-up-a-basic-signalr-hub-in-an-asp-dot-net-core-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
