using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
namespace FinlyticBackend.Hubs;
///
/// SignalR Hub that streams live log messages from microservices to connected web UI clients.
///
public class LogStreamHub : Hub
{
private readonly ILogger _logger;
public LogStreamHub(ILogger logger)
{
_logger = logger;
}
public async Task JoinServiceLogs(string serviceName)
{
if (!string.IsNullOrWhiteSpace(serviceName))
{
await Groups.AddToGroupAsync(Context.ConnectionId, serviceName);
_logger.LogInformation("[LogStreamHub] Client {ConnectionId} joined log stream for {ServiceName}", Context.ConnectionId, serviceName);
}
}
public async Task LeaveServiceLogs(string serviceName)
{
if (!string.IsNullOrWhiteSpace(serviceName))
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, serviceName);
_logger.LogInformation("[LogStreamHub] Client {ConnectionId} left log stream for {ServiceName}", Context.ConnectionId, serviceName);
}
}
public override async Task OnConnectedAsync()
{
_logger.LogInformation("[LogStreamHub] Client connected: {ConnectionId}", Context.ConnectionId);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
_logger.LogInformation("[LogStreamHub] Client disconnected: {ConnectionId}", Context.ConnectionId);
await base.OnDisconnectedAsync(exception);
}
}