Call signalr from another controller

asp.net-mvc-4, c#, signalr

Solution

You need to call `addNewMessageToPage` in your `Post` action method.

var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
hubContext.Clients.All.addNewMessageToPage(chat.Name, chat.Message);

Then in your JS file:

var chatHub = $.connection.chatHub;

chatHub.client.addNewMessageToPage= function (name, message) {
    //Add name and message to the page here
};
$.connection.hub.start();

Problem

I have a ChatHub sending message to the client: ``` public class ChatHub : Hub { public void Send(string name, string message) { Clients.All.addNewMessageToPage(name, message); } } ``` How can I call the Send function to broadcast the message to all client from `another controller`? I have tried this: ``` [HttpPost] public void Post(Chat chat) { var hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>(); hubContext.Clients.All.Send(chat.Name, chat.Message); } ```

Original source