4

I created a simple test app to reproduce a problem I'm having in my main app.

I have the following hub class:

[HubName("testHub")]
public class TestHub : Hub
{
    public TestHub()
    {
        System.Diagnostics.Debug.WriteLine("TestHub instantiated");
    }

    public void RunMe()
    {
        System.Diagnostics.Debug.WriteLine("Client Started");
    }

    public static void Notify(string msg)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<TestHub>();
        hubContext.Clients.All.notify("Hello!");
    }
}

My test web page is:

<form action="javascript: void(0)" method="post">
    <input type="button" value="Do It!" onclick="hitHub()"/>
</form>

<div id="error"></div>
<script type="text/javascript">
    var tHub;    
    $(document).ready(function () {
        tHub = $.connection.testHub;
        tHub.notify = function (msg) {
            alert(msg);
        }
        $.connection.hub.start().done(function () {
            tHub.server.runMe();
        });
    });

    function hitHub() {
        $.ajax({
            type: "POST",
            url: "@Url.Content("~/Hub/Test")" ,
            success: function (data, textStatus, jqXHR) {
            },
            error: function (data, textStatus, jqXHR) {
                $("#error")[0].innerHTML = data.responseText;
                alert("Error notifying hub.");
            }
        });

    }
</script>

And finally, my HubController:

public class HubController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public void Test()
    {
        TestHub.Notify("Got it!");
    }
}

In my Application_Start, I call RouteTable.Routes.MapHubs();

The hub gets instantiated. Then the call to runMe() gets passed to the server. This all works fine.

Where it fails is when I click on the "Do It!" button. hitHub() gets called and my HubController.Test() method gets called. TestHub.Notify("Got it!") gets executed without any errors, however nothing happens on the client.

What did I miss?

Update 1: Based on answer from JcFx, changed the javascript above so that tHub.notify is set before calling $.connection.hub.start(). The problem remains, however.

Update 2: What fiddler sees: What the Fiddler sees

Update 3: When I trace into the MessageBus.Publish() call, I notice tha the Topic has no subscriptions, so the topic never gets scheduled. I'm not sure how at what point I should be checking for the subscriptiont to be made...

Pete
  • 6,585
  • 5
  • 43
  • 69
  • Another thought: Try calling `GlobalHost.ConnectionManager.GetHubContext` and then your `notify` client-side code, from a function that is not static. I'm not sure if the hubcontext exists, or is the same context, in a static method. – Jude Fisher Nov 30 '12 at 16:41
  • Tried that. It didn't work either. I appreciate the ideas... – Pete Nov 30 '12 at 16:50
  • The first one (order of functions) is definitely something you needed to fix. Not sure what else might be wrong - I have almost identical code and it works fine. Sorry it didn't help... – Jude Fisher Nov 30 '12 at 16:52

1 Answers1

3

You're using the wrong syntax:

var tHub;    
$(document).ready(function () {
    tHub = $.connection.testHub;
    tHub.client.notify = function (msg) {
        alert(msg);
    }
    $.connection.hub.start().done(function () {
        tHub.server.runMe();
    });
});

Notice the .client property on the hub to register the callback.

davidfowl
  • 37,120
  • 7
  • 93
  • 103
  • Thanks, David. I was hoping you'd chime in and have the answer and, of course, you did. I assume this is something that changed relatively recently, as most of the examples on the web don't use that syntax. Anyway, thanks. – Pete Nov 30 '12 at 22:34
  • Yep. When in doubt, look at the docs. They're up to date. Also read here http://weblogs.asp.net/davidfowler/archive/2012/11/11/microsoft-asp-net-signalr.aspx. – davidfowl Nov 30 '12 at 22:36
  • You don't believe how many times I glossed over this bit in the docs. Thanks David! – Dan F May 22 '13 at 11:48
  • And how to send a Message only specific user then? I have tried `hubContext.Clients.User(userName).notify(msg);` but this is not working. – imdadhusen Dec 26 '14 at 10:17
  • 1
    @imdadhusen `hubContext.Clients.User(userName).notify(msg);` this works, but first check this article [Click](http://stackoverflow.com/questions/25789873/an-unexpected-behavior-of-authorize-attribute-in-signalr-hub-windows-authentica/28644179#28644179) – Daniel Petrovaliev May 27 '15 at 12:10