2

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);
 }
sudip chand
  • 225
  • 6
  • 14
  • Can you set breakpoint and debug it ? We need more details about the issue. – Jason Pan Sep 08 '21 at 06:39
  • @JasonPan : what is happeing is that, method "NotifyStudentAboutAssignmentCreated" is called for user who is calling the method (that is currently loggedin user) but not for userid present in HashSet userIdList – sudip chand Sep 08 '21 at 07:39
  • 1
    Have you set up the mapping between Connections and User Ids? https://stackoverflow.com/questions/19522103/signalr-sending-a-message-to-a-specific-user-using-iuseridprovider-new-2-0 Also, you may want to send to a SignalR group instead of each user individually. – Sylvain Gantois Sep 10 '21 at 06:05
  • I am facing same issue while using SignalR with server side Balzor... _hubContext.Clients.Client(_SDHubConnectionId).SendAsync("ClientSideMethod", parm1, parm2) is working in MVC .NET 6 project but not in Blazor project... Client side method is not being called... – Bharat Vasant Nov 23 '21 at 10:55

2 Answers2

0

Its difficult to diagnose exactly what is wrong without debugging. I would recommend check the connection state as a start. On the HubConnection class there is this property.

    /// <summary>
    /// Indicates the state of the <see cref="HubConnection"/> to the server.
    /// </summary>
    public HubConnectionState State => _state.OverallState;

Its sometimes useful to ever create an endpoint that just returns this so that you can check it whenever your app is running.

I see that you write to the console with an override on OnConnectedAsync. Have you checked the console. Do you ever see this?

Hub could not be connecting for something as simple as an incorrect url.

drim4591
  • 68
  • 7
0

It looks like there is a problem with SignalR authentication. It can either be that the cookie name or the cookie value you are sending from the client are incorrect, or the server is not configured properly to handle incoming authentication cookies.

Sherif Elmetainy
  • 4,034
  • 1
  • 13
  • 22