Search code examples
signalrsignalr-hub

SignalR connected client in Clients.User(..) shouldn't exist


In my SignalR hub, I use the following method to check whether a user has an active connection:

var receivingClient = Clients.User(receiver);

        if (receivingClient != null)
        {

But I also track the online users manually over OnConnected \ OnDisconnected (in a ConcurrentDictionary). Now even when I shut down everything and start the server from scratch (e.g. IISExpress from VS), the above code part returns a result for a connection that doesn't exist. Let's say I send from User A to user B. I start the server, go online with user A, then send a message to B: The above code returns a Microsoft.AspNetCore.SignalR.Internal.UserProxy<mySite.Services.ChatHub>.

I don't get it. Is it wrong to check for existing client connections with a null check? Should I exclusively rely on my manual tracking?

Thanks for some insight!

(PS: This is all on the same server - no load balancing / sharding)


Solution

  • Clients.User(receiver) returns a type that is used to invoke methods for the given user. It doesn't have anything to do with whether the user you pass in exists or not.

    Is it wrong to check for existing client connections with a null check? Should I exclusively rely on my manual tracking?

    Yes. Use manual tracking.