How to receive return values from a SignalR message?

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...


Calling SignalR Service from JS and having a return value is simple! (Sorry I didn't read the original question well, probably was very late at night). You can do it nicely:

<script type="text/javascript">
    $(function () {

        $.connection.hub.url = "http://localhost:8080/SignalR";

        // Declare a proxy to reference the hub.
        var myHub = $.connection.myHub;

        // Start the connection.
        $.connection.hub.start().done(function () {

            //Once connected, can call methods with return parameters:
            myHub.server
                .getValue("userId")
                .done(function (result) {
                    $('#resultText').val(result); //display in textbox the return value of a call to public string GetValue(string userId) 
                });
        });
    });
</script>

Works in SignalR 2.0


On the server:

    public bool TestBoolRetVal()
    {
        return true;
    }

On the c# client:

 bool bres = HubProxy.Invoke< bool >("TestBoolRetVal").Result;

Of course you can have any signature defined.

Tags:

Signalr