I got stuck with this problem for multiple days and have been reading the implementation on the https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-8.0 of course it couldn’t help me.
My problem was not able to update the component from the code below when using Clients.Caller or Clients.Client. I was able to update the component using Clients.All but it updated all connected clients for my visitors which I didn’t want.
I only wanted to update status for the same visitor/user only and it didn’t work for me.
If anyone here know how to solve this problem, please let me know. Thank you.
using DocZ.Models.Security;
using DocZ.Services.Data;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace BlazorWebAssemblySignalRApp.Server.Hubs
public class BannerMyUsageHub : Hub
private readonly ILogger<BannerMyUsageHub> _logger;
private readonly IDatabaseService _databaseService;
public BannerMyUsageHub(IDbContextFactory<ApplicationDbContext> dbContextFactory, IDatabaseService databaseService, ILogger<BannerMyUsageHub> logger)
_databaseService = databaseService;
/// Location to receive SignalR request from client app or within the backend
/// <param name="userName"></param>
public async Task MyUsageMessageTouserName(string userName)
if (String.IsNullOrEmpty(userName) == false)
//Get total record from database
int totalMyUsage = await _databaseService.BannerMyUsageCountPending(userName);
string messageToViewer = "";
messageToViewer = $"You have {totalMyUsage} open chart{UtilityMethods.Pluralization(totalMyUsage)}.";
//Blazor didn't refresh the header.
await Clients.Caller.SendAsync("ReceiveMessage", messageToViewer);
//Blazor couldn't load other browser tab from the say user.
//await Clients.Client(Context.ConnectionId).SendAsync("ReceiveMessage", messageToViewer);
//Can't use this becuase it would update all visitors.
//await Clients.All.SendAsync("ReceiveMessage", messageToViewer);
//Didn't work at all... await Clients.User(userName).SendAsync("ReceiveMessage", messageToViewer);
<code>
using DocZ.Data;
using DocZ.Helpers;
using DocZ.Models.Security;
using DocZ.Services.Data;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace BlazorWebAssemblySignalRApp.Server.Hubs
{
public class BannerMyUsageHub : Hub
{
private readonly ILogger<BannerMyUsageHub> _logger;
private readonly IDatabaseService _databaseService;
public BannerMyUsageHub(IDbContextFactory<ApplicationDbContext> dbContextFactory, IDatabaseService databaseService, ILogger<BannerMyUsageHub> logger)
{
_logger = logger;
_databaseService = databaseService;
}
/// <summary>
/// Location to receive SignalR request from client app or within the backend
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public async Task MyUsageMessageTouserName(string userName)
{
if (String.IsNullOrEmpty(userName) == false)
{
//Get total record from database
int totalMyUsage = await _databaseService.BannerMyUsageCountPending(userName);
string messageToViewer = "";
if (totalMyUsage > 0)
{
messageToViewer = $"You have {totalMyUsage} open chart{UtilityMethods.Pluralization(totalMyUsage)}.";
}
//Blazor didn't refresh the header.
await Clients.Caller.SendAsync("ReceiveMessage", messageToViewer);
//Blazor couldn't load other browser tab from the say user.
//await Clients.Client(Context.ConnectionId).SendAsync("ReceiveMessage", messageToViewer);
//Can't use this becuase it would update all visitors.
//await Clients.All.SendAsync("ReceiveMessage", messageToViewer);
//Didn't work at all... await Clients.User(userName).SendAsync("ReceiveMessage", messageToViewer);
}
}
}
}
</code>
using DocZ.Data;
using DocZ.Helpers;
using DocZ.Models.Security;
using DocZ.Services.Data;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace BlazorWebAssemblySignalRApp.Server.Hubs
{
public class BannerMyUsageHub : Hub
{
private readonly ILogger<BannerMyUsageHub> _logger;
private readonly IDatabaseService _databaseService;
public BannerMyUsageHub(IDbContextFactory<ApplicationDbContext> dbContextFactory, IDatabaseService databaseService, ILogger<BannerMyUsageHub> logger)
{
_logger = logger;
_databaseService = databaseService;
}
/// <summary>
/// Location to receive SignalR request from client app or within the backend
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public async Task MyUsageMessageTouserName(string userName)
{
if (String.IsNullOrEmpty(userName) == false)
{
//Get total record from database
int totalMyUsage = await _databaseService.BannerMyUsageCountPending(userName);
string messageToViewer = "";
if (totalMyUsage > 0)
{
messageToViewer = $"You have {totalMyUsage} open chart{UtilityMethods.Pluralization(totalMyUsage)}.";
}
//Blazor didn't refresh the header.
await Clients.Caller.SendAsync("ReceiveMessage", messageToViewer);
//Blazor couldn't load other browser tab from the say user.
//await Clients.Client(Context.ConnectionId).SendAsync("ReceiveMessage", messageToViewer);
//Can't use this becuase it would update all visitors.
//await Clients.All.SendAsync("ReceiveMessage", messageToViewer);
//Didn't work at all... await Clients.User(userName).SendAsync("ReceiveMessage", messageToViewer);
}
}
}
}