3

I want to implement a SignalR logging, where the server will regularly ping all it's clients for any accumulated logs.

Microsoft isn't exactly clear on this: docs

What I don't understand is if there is a way to get results from all clients at once. Something like:

var logsFromAllClients = _hubContext.Clients.All.SendLogs();

However this obviously doesn't work, because Clients.All is simply my strongly-typed client T. Which means that it can only get results from one client. I'm curious as to what will happen if invoked with multiple active clients?

Another idea is to iterate over all active connections and request each client's logs individually. As far as I can tell this is also not supported outside of the box. Would I be able to store active connection ids in a collection and iterate over them to achieve this.

Is there anything else I'm missing?

1 Answer 1

1

There's no mechanism on the SignalR server to send a message to more than one client simultaneously and get a return value from each. ISingleClientProxy is for invoking methods, with a return value, on a single client. This can be obtained from IHubClients.Client(string connectionId) for example, but only for a single client connection id at a time and there's no built-in way to iterate through all open connections.

I suppose you could cache connection IDs yourself as they subscribe and iterate through them by getting their ISingleClientProxy, but this would not be a great practice. You have no control over when or whether a client will respond while you wait to send to the next client in the loop and the client list could easily change while the loop iterates.

A better practice would be to just blast them all a single message instructing them to send you their logs, via _hubContext.Clients.All.SendAsync("SendLogs"). The client-side SendLogs method invoked on each client machine will then turn around and call an additional server-side method in order to actually provide their logs. You'll need to handle these passively as they come in and realize they will arrive in an unpredictable order. I would suggest a standard API POST for this rather than a SignalR invocation.

Also be careful as you could get a LOT of data really quickly depending on how much this is expected to scale.

Sign up to request clarification or add additional context in comments.

6 Comments

Isn't Groups supposed to be if I need to segregate clients? For example I have consumers and producers and I need to send messages to only one type of clients? Or am I drastically misunderstanding its purpose?
@Alex well that's certainly a use case but I think its purpose is more broadly just to assign connections to a group so that you can send a message to all members of that group simultaneously - which is exactly what you want to do. It just so happens that your group consists of everyone. Connections can be in multiple groups and groups can overlap so I don't see any downside to making an "everyone" group to do what you want to do here.
@alex actually you can just call _hubContext.Clients.All.SendAsync() with the remote method name. You don't even need to make an everyone group. Revising my answer.
You can't invoke multiple clients to get a return value, either in .NET6 or later. IClientProxy doesn't have any method for this. ISingleClientProxy has InvokeCoreAsync which allows a return value but only works for one client at a time. For multiple clients, as I suggest above you should send a message to them all to prompt them to send their logs back via a REST API call. Even if you could iterate over multiple ISingleClientProxys that's not a great idea. You have no control over when or whether they will respond. Better to prompt them to send logs and receive them passively.
Thanks for clarifying in details. Seems to me that I should reconsider my approach.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.