0

I tried doing the method call like here

But for some reason it is not working. It is working fine when I am calling from the view.

My purpose is to do a function which fetches large amount of data from the DB and do functions with it and updates the progress as percentage on progress bar on same view. I have implemented everything else but couldn't find a faster to get the data live from the controller to view on each successful completion of the function. I am thankful if anybody else could help me correct this method or suggest a more suitable way for my purpose.

If you need more details, please ask.

Controller:

    [HttpPost]
    public ActionResult SendMessge()
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<SearchHub>();
        hubContext.Clients.All.addNewMessageToPage("New Message");
        return Json("Success"); ;
    }

SearchHub.cs:

namespace SearchHuB
{
    public class SearchHub : Hub
    {
        public void Send(string msg)
        {
            Clients.All.addNewMessageToPage(msg);
        }
    }
}

Script:

<script>
 $(document).ready(function () {
                sendmessage();
                
            });
 function sendmessage() {
                 
                $.ajax({
                    url: '@Url.Action("SendMessge", "Search", new { })',
                    type: 'POST',
                    success: function (result) {

                    }, error: function (jqXHR, exception) {

                    }
                });
            }
var chat = $.connection.searchHub;
        chat.client.addNewMessageToPage = function (m) {
            console.log(m)
        }
         
        var notificationHub = $.connection.searchHub;
        $.connection.hub.start().done(function () {
            console.log('Notification hub started');
        });
<script>
       

It does work when I call from same view like this:

        var chat = $.connection.searchHub;
        chat.server.send("new message")
Aneesh
  • 205
  • 1
  • 11

0 Answers0