javascriptmodel-view-controllersignalrsignalr-hubsignalr.client

Using SignalR Hub inside MCV Partial Views


I am creating a console view that uses 2 partial views. Each partial view encapsulates one kind of data and consumes its own SignalR data.

If I include just one partial view in the main view, the partial view works perfectly. It doesn't matter which view I include.

If I include both, only the first partial view is refreshed. The second view hub function is not invoked.

The js function attached to the hub as the following simple structure and it is defined inside the partial view:

$(function () {
    //same for both views
    var hub = $.connection.someHub; 

    //Each view uses a different function
    hub.client.partialupdateX = function (data) {
        // Add the message to the page.

        tabledata = JSON.parse(data);
     
        table.ajax.reload();
    };
    $.connection.hub.start();
});

The hub is the same for both views, but each view attach to a different function. At the server side, both functions are triggered on the same cycle.

Do I have to break isolation and put the hub js functions at the top level page? Am I missing something here?


Solution

  • Turn out to be very simple. I can only start hub once, so i put the following function in the main page, after including both partial views.

    $(function () {         
         $.connection.hub.start();
    });
    

    All the hub functions can be bind inside the partial views.