using Microsoft.AspNetCore.SignalR; namespace Api.SignalR { public class SignalRSendService(IHubContext hubContext, ILogger logger) : BackgroundService { #region Override ExecuteAsync protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { // For example, wait 10 seconds between messages. await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken); // Log or do any work here. logger.LogInformation("Background service sending message at: {Time}", DateTime.Now); // Send a message to all connected clients. await hubContext.Clients.User("user1234").SendAsync( "ReceiveMessage", // This is the client method name. "Background Service", // Example sender. $"Hello from the background task at {DateTime.Now:F}", // The message. stoppingToken); } } #endregion } }