Reputation: 8499
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);
}
Upvotes: 15
Views: 10660
Reputation: 15188
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();
Upvotes: 25