Context
I have the following workflow to let the server query data from a connected client:
// Server-side code
async Task<string> RequestDataFromClient(string groupName)
{
var responseId = GenerateUniqueId();
// let the clients know that we want something
await hubContext.Clients.Group(groupName).Request(responseId);
try
{
return await WaitForIncomingCall(responseId);
}
catch(TimeoutException)
{
return GetDataFromFallbackMethod();
}
}
The server first sends a Request
to all clients in a given SignalR group. Then it waits for any client to call the Respond
hub method with the given responseId
using WaitForIncomingCall
. WaitForIncomingCall
has the following signature
Task<string> WaitForIncomingCall(responseId)
If no client calls the Respond
hub method after a period of time, WaitForIncomingCall
throws a TimeoutException
. If such an exception occurs, the server will use a fallback method to retrieve the data (e.g., from a cache).
Problem
If no client exists in the given SignalR group, the server will wait until the timeout is exceeded until it starts the fallback method. This will produce a significant delay for the caller of RequestDataFromClient
. I would rather directly invoke the fallback method if there is no client in the SignalR group. Hence, if there is no one who could potentially respond, don't even ask.
Question
How can I find out if a SignalR group is empty or if the Request
call could potentially have reached a connected client? Manually tracking connections does not seem to be a good idea as soon as load balancing comes into play.