Question

I am trying to send a message to the group and I cannot get it working. I add the users to the group but the message is not sent, though using Clients.All works. Here is my setup.

Javascript called to connect to the hub, it fetches the users in the group and returns them as users in the chatroom and then sends a joinRoom to the server so I can add the user to the group and send a message back from the server to the client stating they have joined.

Javascript To Connect

$.connection.hub.start()
                    .done(function () {
                        chatHub.server.getConnectedUsers("MyChat") //return user list
                                    .done(function (connectedUsers) {
                                        ko.utils.arrayForEach(connectedUsers, function (item) {
                                            users.contacts.push(new chatR.user(item.Username));
                                        });
                                    }).done(function () {
                                              chatHub.server.joinRoom("MyChat", "My Room")
                                             .done()
                                             .fail(function(){ alert('failed to join group')}); //join the group
                                        });
                        });

Server Side JoinRoom

public async Task JoinRoom(string room, string displayName)
    {
        // context variables
        var name = Context.User.Identity.Name;
        var connectionId = Context.ConnectionId;

        // new group
        var group = new SignalGroup(room, displayName, SignalGroupType.Chatroom);

        // adding relation to storage
        _manager.AddGroup(name, group); <-- adds to database

        // anouncing the room was joined
        Clients.Group(room).joinedRoom(name); //<-- This does not work
        //Clients.All.joinedRoom(name); <-- This works
        //Clients.OthersInGroup(room).joinedRoom(name);

        // add group to SignalR
        await Groups.Add(room, connectionId); // <-- why does this have to be last? when I move this before the _manager.AddGroup it never sends the client message?
    }

So the Clients.Group(room).joinedRoom(name) does not work, i get no error message and the client never receives the message. Here is the client function.

Client Side JoinedRoom

chatHub.client.joinedRoom = function (name) {
            var connectedUser = new chatR.user(name);
            users.contacts.push(connectedUser);
            chat.messages.push(new chatR.chatMessage("System", name + " joined.", new Date()));
        };

For a "Bonus" here is my SendChatMessage method that too works when I send to ALL but not to a group.

public void SendChatMessage(string room, string message)
    {
        // context variables
        var name = Context.User.Identity.Name;
        var user = _manager.GetUser(name);

        if (user.IsInGroup(room))
        {
            // tells clients to addChatMessage
            //Clients.All.addChatMessage(name, message, DateTime.Now);
            Clients.Group(room).addChatMessage(name, message, DateTime.Now);
        }
    }

So my main question is, Why am I not able to send to the groups? I am clearly adding them to the group and sending a message to the group?

Second question is, Why does the JoinRoom method have to have the add to group call very last or it seem it does not work at all even with All?

If you have any helpful links for this stuff that would be great as well, I have read all the MS documentation and several tutorials which leaves me even more baffled on why this is not working.

EDIT: I added a Failed check on the joinRoom call to see if I get anything back and it appears as if SignalR is failing to join the group itself. I am not sure how I would troubleshoot this though.

Was it helpful?

Solution

You got the order of parameters wrong. It should be

Groups.Add(connectionId, room);

Also, if you want to use the group for this user in the same method, you need to add the user to the group first and await the call. Otherwise it's not really necessary to await.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top