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);
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With