In modern applications, real-time communication is no longer optional. Whether it’s remote agents, background services, live dashboards, or command execution systems — instant two-way communication is critical.

In this article, we’ll build a Real-Time Agent–Server Architecture using SignalR in ASP.NET Core, understand why SignalR beats REST, and walk through a live working C# demo.

What Is Agent–Server Architecture?

An Agent–Server architecture consists of:

🧠 Server (Central Controller)

🤖 Agent (Client / Worker)

Real-world Use Cases

Why SignalR Over REST APIs?

FeatureREST APISignalR
CommunicationRequest–ResponseBi-directional
Real-time❌ No✅ Yes
Server → Client Push❌ No✅ Yes
Connection StateStatelessPersistent
LatencyHighVery Low
Agent ControlPoorExcellent

REST is pull-based. SignalR is push-based.

If the server must command agents instantly, REST simply isn’t enough.

What Is SignalR?

SignalR is a real-time communication framework built by Microsoft for ASP.NET.

It enables:

Perfect for live agent execution systems.

Real-Time Execution & Response Flow

🔄 Execution Lifecycle

  1. Agent starts and connects to SignalR Hub

  2. Server detects connected agent

  3. Server sends command (SQL / job / task)

  4. Agent executes the task

  5. Agent sends JSON response

  6. Server processes or broadcasts result

💡 No polling. No delays. No refresh.

Architecture Overview

┌──────────────┐        SignalR        ┌──────────────┐
│   Server     │  ─────────────────▶  │    Agent     │
│ ASP.NET Core │  ◀─────────────────  │  Console App │
└──────────────┘   Real-Time JSON      └──────────────┘

1: Create SignalR Hub (Server)

📁 AgentHub.cs

using Microsoft.AspNetCore.SignalR;

public class AgentHub : Hub
{
    public async Task SendCommand(string agentId, string command)
    {
        await Clients.User(agentId)
                     .SendAsync("ExecuteCommand", command);
    }

    public async Task ReceiveResult(string agentId, string result)
    {
        Console.WriteLine($"Agent {agentId}: {result}");
    }
}

Step 2: Register SignalR in ASP.NET Core

📁 Program.cs

builder.Services.AddSignalR();

app.MapHub<AgentHub>("/hubs/agent");

Step 3: Agent Console Application (C#)

This agent connects to the server and executes commands live.

📁 Program.cs (Agent)

using Microsoft.AspNetCore.SignalR.Client;

class Program
{
    static async Task Main(string[] args)
    {
        var agentId = "agent1";

        var connection = new HubConnectionBuilder()
            .WithUrl("http://localhost:5000/hubs/agent")
            .WithAutomaticReconnect()
            .Build();

        connection.On<string>("ExecuteCommand", async (command) =>
        {
            Console.WriteLine($"Received: {command}");

            string result = $"Executed command: {command} at {DateTime.Now}";

            await connection.SendAsync(
                "ReceiveResult",
                agentId,
                result
            );
        });

        await connection.StartAsync();
        Console.WriteLine("Agent connected.");

        Console.ReadLine();
    }
}

Step 4: Send Command From Server

await hubContext.Clients
    .User("agent1")
    .SendAsync("ExecuteCommand", "SELECT * FROM Users");

✔ Agent receives instantly
✔ Executes logic
✔ Sends response live

Why This System Is Powerful

✅ True real-time execution
✅ Scales to thousands of agents
✅ No polling or cron jobs
✅ Works with Windows Services
✅ Secure (JWT / headers supported)
✅ Cloud & on-prem friendly

Advanced Enhancements (Next Level)

Final Thoughts

If you’re building:

👉 SignalR is the right foundation.

It transforms your app from request-based to event-driven, unlocking true real-time power.