using Microsoft.AspNetCore.SignalR;
using NexusReader.Application.Abstractions.Messaging;
using NexusReader.Infrastructure.RealTime;
namespace NexusReader.Infrastructure.RealTime;
///
/// SignalR implementation of .
/// Uses to push progress updates to all of a user's connected devices.
///
internal sealed class SignalRSyncBroadcaster : ISyncBroadcaster
{
private readonly IHubContext _hubContext;
public SignalRSyncBroadcaster(IHubContext hubContext)
{
_hubContext = hubContext;
}
///
public async Task BroadcastProgressAsync(
string userId,
string pageId,
DateTime timestamp,
string? excludedConnectionId,
CancellationToken cancellationToken = default)
{
var groupName = $"User_{userId}";
if (!string.IsNullOrEmpty(excludedConnectionId))
{
await _hubContext.Clients
.GroupExcept(groupName, excludedConnectionId)
.SendAsync("ProgressUpdated", pageId, timestamp, cancellationToken: cancellationToken);
}
else
{
await _hubContext.Clients
.Group(groupName)
.SendAsync("ProgressUpdated", pageId, timestamp, cancellationToken: cancellationToken);
}
}
}