javascriptasp.net-coresignalrsignalr-hub

Trying to send a signalR message to a specific logged in user


BACKGROUND: I have set-up a session cookie management implementation in my ASP.NET Core 8.0 web application, the authentication is cookie based. When I login to the application first time, the JWT is extracted and httponly cookie is set up for the application,then I fetch the token expiry time and send it to my SignalR hub from my controller ( included screenshot), the listener is setup in my frontend implementation ( I have used javascript for this and the code is tightly coupled to my web app, no need of CORS )

ISSUE: When i use the

await _hubContext.Clients.Group($"user_{loginResponseDto.User.Id}").SendAsync("SessionExpiryTime", tokenExpiry);

OR

await _hubContext.Clients.User(loginResponseDto.User.Id).SendAsync("SessionExpiryTime", tokenExpiry);

the messages are NOT available on my JS, the hub connection is open and listening, without any errors or exceptions thrown. Only when I use

await _hubContext.Clients.All.SendAsync("SessionExpiryTime", tokenExpiry);

the events are available and the implementation works as expected.

My Login method inside the Auth Controller, LN 71 where i trigger the signalR event

My SignalR hub class

Program.cs

SignalR mapping

IUserIdProvider Interface

EXPECTATION: I want to setup the SignalR expiry time refresh message only for the particular user who is logged in, I have tried to make this implementation work by using the userId and connectionId, but unfortunately other than Clients.All.SendAsync nothing has worked for me so far.

I haven't been able to exactly pinpoint why my messages are not going across when I use Clients.User OR Clients.Group. I have tried numerous Youtube videos and Microsoft SignalR docs and implementations but to no luck. Also, let me know if this is some implementation error from my end, or what I have done here is wrong.

Please let me know if repo access is required to take a look at this issue, or anything else I can assist with to troubleshoot this.


Solution

  • You can try Client instead of User as below:

    await _hubContext.Clients.Client(loginResponseDto.User.Id)
        .SendAsync("SessionExpiryTime", tokenExpiry);