Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR: Invoke method for returnvalue

Tags:

c#

signalr

I've been able to get pretty far using SignalR pushing messages to client and from clients back to server... and that so far has been a blessing.

What I'd like ot be able to do, and this may simply be something SignalR cant handle... is to invoke for a response.

Does SignalR allow responses from invocations? If so, can I make a request to clients, and just use the first response?

I saw references to HubResponse, and the task.result, but I dont quite see how to have a hub method return values from clients.

I played with it quite a bit and either got AggregateException, or invalid cast from Task<Object> to Task<MyType>

like image 941
Ronnyek Avatar asked Nov 22 '25 06:11

Ronnyek


1 Answers

The connected clients are broadcast only. That is the server cannot make requests of them directly. So you would need to do something like have a Hub method where one client can request that someone else fulfill a request, the Hub then broadcasts that request to all connected clients and then whichever client responds first you could broadcast back to the original client. You would need to maintain your own state in the Hub to direct the request/responses to the correct audiences obviously. I would also think that as soon as you receive your first answer back you would want to tell all other clients to not bother/stop trying to answer the question.

I'm not sure I can prescribe an exact architectecture for this without some more details tho. For starters, should any client be able to respond any request or would you categorize clients? If you can categorize clients then you can ensure you only broadcast the request that make sense to them using groups. Next are you basically trying to define an interface of all the questions that can be asked or do you want the set to be able to expand without changing the Hub over time? That would dictate whether you were using strongly typed Hub methods or if you would just use some kind of generic AskQuestion(string questionId). If the former then you would probably not use groups as previously mentioned, but instead just setup distinct hubs per request category.

like image 170
Drew Marsh Avatar answered Nov 24 '25 18:11

Drew Marsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!