DEV Community

Cover image for 📡 Real-Time Communication with SignalR in ASP.NET Core Complete Setup Guide
Hardik Jariwala
Hardik Jariwala

Posted on

3 2 2 2 1

📡 Real-Time Communication with SignalR in ASP.NET Core Complete Setup Guide

Want to build a real-time chat app or push live notifications? In this blog, I’ll walk you through setting up SignalR from scratch using ASP.NET Core and a .NET console client. Perfect for beginners or those revisiting SignalR.

🔧 Step 1: Create the SignalR Server Project

Create an ASP.NET Core Web API project and add package

dotnet add package Microsoft.AspNetCore.SignalR
Enter fullscreen mode Exit fullscreen mode

🧠 Step 2: Create a SignalR Hub

Create a new class ChatHub.cs inside the project:

using Microsoft.AspNetCore.SignalR;

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

🌐 Step 3: Configure the SignalR Hub in Program.cs

Update Program.cs to register the SignalR service and endpoint:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
builder.Services.AddControllers();
var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

// Add this line to map the hub
app.MapHub<ChatHub>("/chatHub");

app.Run();
Enter fullscreen mode Exit fullscreen mode

🧪 Step 4: Run the Server

dotnet run
Enter fullscreen mode Exit fullscreen mode

📥 Step 5: Create a .NET Console Client

Create a new console app and add SignalR client package

dotnet add package Microsoft.AspNetCore.SignalR.Client
Enter fullscreen mode Exit fullscreen mode

🧑‍💻 Step 6: Build the Client Logic

Replace Program.cs content with:

using Microsoft.AspNetCore.SignalR.Client;

var connection = new HubConnectionBuilder()
    .WithUrl("https://<your localhost>/chatHub") your server
    .WithAutomaticReconnect()
    .Build();

// Receive messages
connection.On<string, string>("ReceiveMessage", (user, message) =>
{
    Console.WriteLine($"{user}: {message}");
});

try
{
    await connection.StartAsync();
    Console.WriteLine("Connected to SignalR Hub.");
}
catch (Exception ex)
{
    Console.WriteLine($"Connection failed: {ex.Message}");
    return;
}

// Sending messages
while (true)
{
    Console.Write("Enter your name: ");
    var user = Console.ReadLine();

    Console.Write("Enter your message: ");
    var message = Console.ReadLine();

    try
    {
        await connection.InvokeAsync("SendMessage", user, message);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error sending message: {ex.Message}");
    }
}
Enter fullscreen mode Exit fullscreen mode

✅ Step 7: Test the Communication

  • Run the server project:
cd ../SignalRServer
dotnet run
Enter fullscreen mode Exit fullscreen mode
  • Run the client project in a new terminal:
cd ../SignalRClient
dotnet run
Enter fullscreen mode Exit fullscreen mode

Type your name and message in the console. If everything is set up correctly, you’ll see the message echoed back from the hub.

🧪 Test Multiple Clients

Open two terminals and run the client in each. You’ll see real-time communication between both.

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (0)

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping