feat: implement Phase 4 — SignalR real-time updates
Backend: - RmmHub with typed IRmmHubClient interface (AgentMetricsUpdated, AgentStatusChanged, CommandResultUpdated) - JoinAgentGroup / LeaveAgentGroup for per-agent subscriptions - AgentGrpcService now pushes to SignalR after every Heartbeat and CommandResult - Program.cs maps /hubs/rmm Frontend: - useSignalR hook with exponential backoff reconnect (0s/2s/10s/30s) - useGlobalSignalR: invalidates agents query on AgentStatusChanged - useAgentSignalR: joins agent group, invalidates metrics/tasks on updates - DashboardPage: live agent status updates via SignalR - AgentDetailPage: live metrics/command results + connection status indicator Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using System.Text.Json;
|
||||
using Grpc.Core;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NexusRMM.Api.Hubs;
|
||||
using NexusRMM.Core.Models;
|
||||
using NexusRMM.Infrastructure.Data;
|
||||
using NexusRMM.Protos;
|
||||
@@ -13,11 +15,13 @@ public class AgentGrpcService : AgentService.AgentServiceBase
|
||||
{
|
||||
private readonly RmmDbContext _db;
|
||||
private readonly ILogger<AgentGrpcService> _logger;
|
||||
private readonly IHubContext<RmmHub, IRmmHubClient> _hub;
|
||||
|
||||
public AgentGrpcService(RmmDbContext db, ILogger<AgentGrpcService> logger)
|
||||
public AgentGrpcService(RmmDbContext db, ILogger<AgentGrpcService> logger, IHubContext<RmmHub, IRmmHubClient> hub)
|
||||
{
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
_hub = hub;
|
||||
}
|
||||
|
||||
public override async Task<EnrollResponse> Enroll(EnrollRequest request, ServerCallContext context)
|
||||
@@ -82,6 +86,22 @@ public class AgentGrpcService : AgentService.AgentServiceBase
|
||||
}
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
// SignalR: Metriken an agent-Gruppe pushen
|
||||
await _hub.Clients.Group($"agent-{agentId}")
|
||||
.AgentMetricsUpdated(request.AgentId, new
|
||||
{
|
||||
CpuUsagePercent = request.Metrics?.CpuUsagePercent ?? 0,
|
||||
MemoryUsagePercent = request.Metrics?.MemoryUsagePercent ?? 0,
|
||||
MemoryTotalBytes = request.Metrics?.MemoryTotalBytes ?? 0,
|
||||
MemoryAvailableBytes = request.Metrics?.MemoryAvailableBytes ?? 0,
|
||||
UptimeSeconds = request.Metrics?.UptimeSeconds ?? 0,
|
||||
});
|
||||
|
||||
// SignalR: Status-Änderung an alle Clients pushen
|
||||
await _hub.Clients.All
|
||||
.AgentStatusChanged(request.AgentId, "Online", DateTime.UtcNow.ToString("O"));
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -102,6 +122,11 @@ public class AgentGrpcService : AgentService.AgentServiceBase
|
||||
});
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
// SignalR: Command-Ergebnis an agent-Gruppe pushen
|
||||
await _hub.Clients.Group($"agent-{request.AgentId}")
|
||||
.CommandResultUpdated(request.CommandId, request.AgentId, request.Success, request.ExitCode);
|
||||
|
||||
return new CommandResultResponse();
|
||||
}
|
||||
|
||||
|
||||
16
Backend/src/NexusRMM.Api/Hubs/IRmmHubClient.cs
Normal file
16
Backend/src/NexusRMM.Api/Hubs/IRmmHubClient.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace NexusRMM.Api.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// Typed SignalR client interface — definiert was der Server zum Frontend pushen kann.
|
||||
/// </summary>
|
||||
public interface IRmmHubClient
|
||||
{
|
||||
/// <summary>Neue Metriken für einen Agent verfügbar (an agent-Gruppe gepusht)</summary>
|
||||
Task AgentMetricsUpdated(string agentId, object metrics);
|
||||
|
||||
/// <summary>Agent-Status hat sich geändert (an alle Clients gepusht)</summary>
|
||||
Task AgentStatusChanged(string agentId, string status, string lastSeen);
|
||||
|
||||
/// <summary>Command-Ergebnis verfügbar (an agent-Gruppe gepusht)</summary>
|
||||
Task CommandResultUpdated(string taskId, string agentId, bool success, int exitCode);
|
||||
}
|
||||
38
Backend/src/NexusRMM.Api/Hubs/RmmHub.cs
Normal file
38
Backend/src/NexusRMM.Api/Hubs/RmmHub.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace NexusRMM.Api.Hubs;
|
||||
|
||||
public class RmmHub : Hub<IRmmHubClient>
|
||||
{
|
||||
private readonly ILogger<RmmHub> _logger;
|
||||
|
||||
public RmmHub(ILogger<RmmHub> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>Frontend tritt der Gruppe für einen bestimmten Agent bei</summary>
|
||||
public async Task JoinAgentGroup(string agentId)
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, $"agent-{agentId}");
|
||||
_logger.LogDebug("Client {ConnectionId} joined group agent-{AgentId}", Context.ConnectionId, agentId);
|
||||
}
|
||||
|
||||
/// <summary>Frontend verlässt die Gruppe für einen Agent</summary>
|
||||
public async Task LeaveAgentGroup(string agentId)
|
||||
{
|
||||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"agent-{agentId}");
|
||||
}
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
_logger.LogInformation("SignalR client connected: {ConnectionId}", Context.ConnectionId);
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
public override async Task OnDisconnectedAsync(Exception? exception)
|
||||
{
|
||||
_logger.LogInformation("SignalR client disconnected: {ConnectionId}", Context.ConnectionId);
|
||||
await base.OnDisconnectedAsync(exception);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NexusRMM.Api.GrpcServices;
|
||||
using NexusRMM.Api.Hubs;
|
||||
using NexusRMM.Infrastructure.Data;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -43,5 +44,6 @@ if (app.Environment.IsDevelopment())
|
||||
app.UseCors();
|
||||
app.MapGrpcService<AgentGrpcService>();
|
||||
app.MapControllers();
|
||||
app.MapHub<RmmHub>("/hubs/rmm");
|
||||
|
||||
app.Run();
|
||||
|
||||
Reference in New Issue
Block a user