Search code examples
c#asp.net-mvcdatabaseentity-frameworksignalr

SignalR notification never reload the page


I was using signalR to manage database changes, when it happens I want to update the page to other users so that they see the change. But what I've done so far always loads, here is the code:

INDEX

@section scripts{  
    <script src="~/Scripts/jquery.signalR-2.4.3.min.js"></script>  
    <script src="/signalr/hubs"></script>  
    <script type="text/javascript">  
        $(function () {  
            var hubNotify = $.connection.Connection4Hub;  
  
            $.connection.hub.start().done(function () {  
                getAll();
            });  
  
            hubNotify.client.GetUpdateData = function () {  
                getAll();
            };
        });  
                          
        function getAll() {  
            var model = $('#dataModel');  
            $.ajax({  
                url: '/Manage/GetUpdateData',  
                contentType: 'application/html ; charset:utf-8',  
                type: 'GET',  
                dataType: 'html',  
                success: function(result) { model.empty().append(result); }  
            });                           
            location.reload();
        }  
    </script>  
}      

Connect4Hub

public class Connect4Hub : Hub
    {
        public static void BroadcastData()
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<Connect4Hub>();
            context.Clients.All.GetUpdateData();
        }
    }

MANAGE

public ActionResult GetUpdateData()
        {
            return PartialView("Partial_Index", db.Matches.ToList());
        }

UPDATE

I found that the method below is never call, now the page never refresh

hubNotify.client.GetUpdateData = function () {  
                getAll();
            };

Solution

  • The SignalR Hubs API enables you to make remote procedure calls (RPCs) from a server to connected clients and from clients to the server. In server code, you define methods that can be called by clients, and you call methods that run on the client.

    In client code, you define methods that can be called from the server, and you call methods that run on the server. SignalR takes care of all of the client-to-server plumbing for you.client can call hub inherited method only.

    Call BroadcastData methods that run on the server:

    hubNotify.client.BroadcastData = function () {  
                                            getAll();
                                     };
    

    Ref: Link