Here I have blazor server app in which I have signalR hub called SessionChatHub
, and it contains method AssignmentCreatedNotification(HashSet<string> userIdList, string name)
which receives userIdList (userIdList is identity id accessed from database AspNetUsers table) and name as parameter and send the name to all the userid of userIdList parameter.
Now the problem is that even after getting values in userIdList
and name
parameter hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);
is not fired that is NotifyStudentAboutAssignmentCreated(string name)
is not called.
But when there is Clients.All.SendAsync("ReceiveAssignmentCreatedNotification", name);
NotifyStudentAboutAssignmentCreated(string name) method is called,but it is not calling on await Clients.User(userId).SendAsync("ReceiveAssignmentCreatedNotification", name);
userIdList: (userIdList is identity id accessed from database AspNetUsers table)
Below is Hub
public class SessionChatHub:Hub
{
public async Task AssignmentCreatedNotification(HashSet<string> userIdList, string name)
{
foreach (var userId in userIdList)
{
await Clients.User(userId).SendAsync("ReceiveAssignmentCreatedNotification", name);
}
}
public async override Task OnConnectedAsync()
{
Console.WriteLine($"{Context.ConnectionId} connected");
}
public override async Task OnDisconnectedAsync(Exception e)
{
Console.WriteLine($"Disconnected {e?.Message} {Context.ConnectionId}");
await base.OnDisconnectedAsync(e);
}
}
Below is my oninitialized where we receive signalr hub
protected async override void OnInitialized()
{
var container = new CookieContainer();
var cookie = new Cookie()
{
Name = ".AspNetCore.Identity.Application",
Domain = "localhost",
Value = CookiesProvider.Cookie
};
container.Add(cookie);
hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/sessionchathub"), options =>
{
options.Cookies = container;
}).Build();
hubConnection.ServerTimeout = TimeSpan.FromMinutes(60);
hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);
await hubConnection.StartAsync();
}
Method to be call
public void NotifyStudentAboutAssignmentCreated(string name) // This method is not called from oninitialized hubConnection.On<string>("ReceiveAssignmentCreatedNotification", NotifyStudentAboutAssignmentCreated);
{
NotificationHandler.AssignmentNotificationCount = 1;
StateHasChanged();
}
I also have OnAfterRenderAsync
protected async override Task OnAfterRenderAsync(bool firstRender)
{
//if (firstRender)
//{
await jSRuntime.InvokeVoidAsync("AddSelect2ForClassIdInCreateEditTeacherAssignment");
await jSRuntime.InvokeVoidAsync("AddSelect2ForSubjectIdInCreateEditTeacherAssignment");
await jSRuntime.InvokeVoidAsync("AddSelect2ForChapterIdInCreateEditTeacherAssignment");
//}
await base.OnAfterRenderAsync(firstRender);
}