Receiving multiple SignalRMessageAction parameters on a AspNetCore client

Bubba Jones 216 Reputation points
2023-04-11T12:32:11.6933333+00:00

I have the following server side azure function which I am able to invoke from an ASP.NET client.

    [Function("SendToGroup")]
    [SignalROutput(HubName = "serverless", ConnectionStringSetting = "SignalRConnection")]
    public static async Task<SignalRMessageAction> SendToGroup([SignalRTrigger("serverless", "messages", "SendToGroup", "groupName", "message")] SignalRInvocationContext invocationContext, string groupName, string message)
    {
        return new SignalRMessageAction("joinedGroupMessage")
        {
            GroupName = groupName,
            Arguments = new object[] { $"Joined group {groupName}." }
        };
    }

However I am not able to receive the above returned SignalRMessageAction in my AspNetCore client once the above has been executed. I am currently using the following AspNetCore client side code.

        //  This syntax compiles but is not correct; it does not trigger when the server returns the 
        //  SignalRMessageAction
        _connection.On<string, string>("joinedGroupMessage", (groupName, groupMessage) =>
        {
            chatMessages.Text = groupName;
            chatMessagesX.Text = groupMessage;
        });

I am able to invoke other SignalRMessageAction's from the server on my client as long as they only send back 1 parameter. So my SignalR setup and upstream settings is working. My issue is with the above syntax which I am not doing right apparantly.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Developer technologies | ASP.NET | ASP.NET Core
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
161 questions
{count} votes

Accepted answer
  1. Liangying Wei 681 Reputation points Microsoft Employee
    2023-04-27T03:58:09.9633333+00:00

    Oh I see you are using local emulator. So now your client is "user1" right? So you need to update your client code to use user1, _groupData.UserId = "user1"

    UserId is actually inside ConnectionContext so you can also update your AddToGroup code to:

    [Function("AddToGroup")]
            [SignalROutput(HubName = "serverless", ConnectionStringSetting = "AzureSignalRConnectionString")]
            //  string userId is NULL here
            public static async Task<SignalRGroupAction> AddToGroup([SignalRTrigger("serverless", "messages", "AddToGroup", "groupName")] SignalRInvocationContext invocationContext, string groupName)
            {
                //  This does not seem to add the user to the group.  Note I do not have any on the client side for this.  Not sure if I need one?
                return new SignalRGroupAction(SignalRGroupActionType.Add)
                {
                    GroupName = groupName,
                    UserId = invocationContext.UserId
                };
            }
    
    

    And update your client code to await _connection.InvokeAsync("AddToGroup", _groupData.GroupName)

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Liangying Wei 681 Reputation points Microsoft Employee
    2023-04-17T01:54:49.4766667+00:00

    Hi @Bubba Jones , if you specify GroupName in your SignalRMessageAction, it means that you are going to send the message to a group, invoking client method joinedGroupMessage with arguments defined in Arguments, there is only 1 object in the Arguments array however your client is expecting 2 parameters, try changing the client code to:

     _connection.On<string, string>("joinedGroupMessage", (groupMessage) =>
            {
                chatMessagesX.Text = groupMessage;
            });
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.