Search code examples
c#-4.0sessionasp.net-mvc-4signalrsignalr-hub

SignalR Groups not getting data


Hi I am having a problem where my signalR clients who are in a group do not get the information sent.

I have tried this in two ways and both not working.

On web page loaded the client will call the server to get the group name and then call a method on the hub to join the user in that group.

JavaScript.....

        var connectionOpen = false;
        var myHub;
        $(function () {
            myHub = $.connection.myHub;
            myHub.client.showMessage = alertMessage;
            $.connection.hub.start(function () {
                connectionOpen = true;
                joinGroup();
            });
        });

        function joinGroup() {
            $.ajax({
                url: 'Controller/GetGroupName,
                type: 'POST',
                success: function (group) {
                    if (connectionOpen == true)
                        tileHub.server.joinGroup(group);
                }
            });
        }

function alertMessage(string value){
alert(value);
}

Controller

[HttpPost]
public JsonResult GetGroupName()
{
    return Json("Foo");
}

myHub

public void JoinGroup(string groupName)
{
    Groups.Add(Context.ConnectionId, groupName);
}

I have stepped through the code numerous times and I can see that I am connecting ok and joining the group but when I try and send back to the group nothing happens

I am sending to the group using this from my controller

GlobalHost.ConnectionManager.GetHubContext<myHub>().Clients.Group("Foo").showMessage("Hello");

This is the second way I have tried, I have also tried sending to the groups from the hub but still no joy.

any help would be greatly appreciated...

Updated This has been fixed thanks to David Fowler....

This was a bug due to the fact that I was giving the Hub a different name using the HubName attribute and then signalR using the longPolling transport. Removing the attribute allows this to know work. A fix has also been provided please see https://github.com/SignalR/SignalR/issues/1039


Solution

  • Few things:

    1. Make sure you read the "Rejoining Groups" section on this post. http://weblogs.asp.net/davidfowler/archive/2012/11/11/microsoft-asp-net-signalr.aspx.
    2. Do you really need to make an ajax call to figure out the group name from the server? Can't you just render it directly in the page?
    3. Does it fail if you hardcode "Foo" as the group name everywhere?