1

I'm trying to implement SignalR into a simple MVC 5 project. I've worked through this tutorial and it works, but I'm now trying to call a hub method from a separate controller.

I have a view called Generate.cshtml, with this markup and Javascript:

<script type="text/javascript">    
    $(function() {
        var myHub = $.connection.ContosoChatHub;

        myHub.client.writeMessageToScreen = function(message) {
            $('#messages').append(message);
        };               
    });
</script>

<input type="text" id="msg" />
<input type="button" id="broadcast" value="broadcast" />

<ul id="messages" class="round">
</ul>

the hub class is

[HubName("ContosoChatHub")]
public class ContosoChatHub : Hub
{
    public void NewContosoChatMessage(string message)
    {
        Clients.All.NewMessage(message);
    }
}

and then in my controller, I want to call the writeMessageToScreen method - either calling it directly, or via the hub class.

I've read that I can call the Javascript method directly

    public ActionResult Generate()
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<ContosoChatHub>();

        context.Clients.All.writeMessageToScreen("Test");

        return View();
    }

But this isn't working and nothing is written to screen.. It isn't throwing any errors either, it just isn't doing.. anything.

Does anyone have any ideas what's going on? Is there a better way to call Hub methods from a Controller?

1

1 Answer 1

1

You didn't start the connection Update:

In you hub class

public class ContosoChatHub : Hub
{
    public static NewContosoChatMessage(string message)
    {
     var notifyContext = GlobalHost.ConnectionManager.GetHubContext<ContosoChatHub >();
        notifyContext.Clients.All.NewMessage(message);
    }
}

then in you controller

public ActionResult Generate()
    {
        ContosoChatHub.NewContosoChatMessage("Test");

        return View();
    }
$(function() {
        var myHub = $.connection.ContosoChatHub;
        $.connection.hub.start();
        myHub.client.writeMessageToScreen = function(message) {
            $('#messages').append(message);
        };               
    });

This should work for you

Sign up to request clarification or add additional context in comments.

2 Comments

I've added $.connection.hub.start().done(function () { myHub.server.runMe(); }); and this executes a 'runMe()' function in my ConosotoChatHub class fine, that all seems to work. But the method in my Controller class still doesn't work, it still doesn't execute the writeMessageToScreen method.
I get an error in the hub class, that 'Cannot access non-static property 'Clients' in static context'. I don't think I can make the Hub method static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.