Since there was no answer for 12 hours (which is rather unusual), I had to dig myself. I think that I was misled by the answers from SO on related issues, that you should subscribe to all client calls before starting a connection, as mentioned, for example. here . I found in the Hubs API Guide , one section says
Define a method on the client (without generated proxy or when added after invoking the start method)
Thus, after connecting, you can add a client method. The trick is to use the so-called “no generated proxy”. This limitation applies to "generated proxies."
Below is my working example, taken from the SignalR tutorial.
This is the main controller using "with generated proxies":
$.connection.statusHub.client.updateStatus = function (status) { $scope.status = status; $scope.$apply(); } $.connection.hub.start();
This is in a subcontroller using "no generated proxy":
var connection = $.hubConnection(); var proxy = connection.createHubProxy('stockTickerHub'); proxy.on('updateStockPrice', function (stock) { var st = $scope.stocks.firstOfKey(stock.symbol, 'symbol'); st.lastPrice = stock.lastPrice; $scope.$apply(); }); var hub = $.connection.stockTickerHub; connection.start().done(function () { hub.server.getAllStocks().done(function (stocks) { $scope.stocks = stocks; }); });
Note that this does not work if I use "with generated proxies" in the subcontroller as follows:
var hub = $.connection.stockTickerHub; hub.client.updateStockPrice = function (stock) { var st = $scope.stocks.firstOfKey(stock.symbol, 'symbol'); st.lastPrice = stock.lastPrice; $scope.$apply(); }; $.connection.hub.start().done(function () { hub.server.getAllStocks().done(function (stocks) { $scope.stocks = stocks; }); });
To prove the “proxy generated” mode restriction, this code works if I comment on it in the main controller.
By the way, I was so confused by the term with or without a generated proxy in the Guide, and in both cases it is still called xxxProxy. Can't they find a better name? Or does anyone have an explanation?