Search code examples
signalr

How to receive return values from a SignalR message?


I`m calling SignalR from inside a MVC Controller action

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
var ret = context.Clients.Client("Whatever").getValue(userId);

Is there anyway I can get a response from the getValue() method being called on the Client inside the action?

I've tried a few ways but simply can't get any value for ret. I'm pretty sure this doesn't work with a SignalR Hub but couldn't find documentation about this.

One solution I've considered is after receiving the getValue the Client would call a method in the Hub but then I would have to hack my way into getting the response from the Hub into the controller.


Solution

  • SignalR is not an RPC mechanism as much as a message passing mechanism. Because of that, there's no real concept of return values.

    If you want to do it, you're going to hack it. You could simply do something where, in the server, you call getValue() and then the client's response is to make a call to the server with the return value.

    So, for example, in your javascript, you could have something like:

    myHub.client.getValue = function(userId) {
      var retVal;
       ... set your retVal here ...
      myHub.returnValue(userId, retVal)
    }
    

    And then have the server implement a returnValue() method...