I’m using SignalR in my mvc4 web application.
I have a class inheriting from HUB
[HubName("Chat")]
public class ChatHub : Hub ,IDisconnect
{
private void CallMessage(string message)
{
Clients.MessagesRecieved(message);
}
....
In my client js file I wrote
$(function () {
globalChatHub = $.connection.chat;
$.extend(globalChatHub, { MessagesRecieved: function (data) {
alert(data);
}
});
the question is ,
Is it possible to invoke the client side script “MessagesRecieved function” from a code in my HomeController.cs
let’s say somthing like that :
public class HomeController : Controller
{
public ActionResult Index()
{
// this is a test
ChatHub h = new ChatHub();
h.CallMessage("hellow");
}
Sure,
You can call it the same way you call client code:
in the server code just write:
the name of the method is case sensitive.
more info in this link SignalR quick start