I wrote the following code with the hope of adding a user to a group and sending a message to the user's group simultaneously. I can see the connections established per the signalR's blade on my Azure's Portal but I cannot conclude whether or not the user has become a member of the group after returning this function.
[Function("OnConnected")]
[SignalROutput(HubName = "myhub")]
public async Task<object[]> OnConnectedAsync([SignalRTrigger("myhub", "connections", "connected")] SignalRInvocationContext invocationContext)
{
var username = "userA";
var groupName = "groupA";
return
[
new SignalRGroupAction(SignalRGroupActionType.Add)
{
GroupName = groupName,
UserId = username
},
new SignalRMessageAction("NewUserConnected")
{
GroupName = groupName,
Arguments = [userName]
}
];
}
Does the function's implementation above exhibit a right approach to add a user to the group and send a message to the group at the same time?
Note: Before writing this code, I tried to add the user to the group by the following line of code while having the function above report
SignalRMessageActionobject but it threw an exception:
await UserGroups.AddToGroupAsync(userName, groupName);
Apparently signalR's SDK does not like the line above in the context of upstreams.
The code below adds a user to a group and sends a message to the group. The Azure Function
SendMessageis designed to handle POST requests and send messages to a SignalR group named "myGroup" using output binding.It reads the request body to extract
userIdandmessage.It adds the
userIdto the SignalR group "myGroup".It sends the
messageto the SignalR group "myGroup".Azure Functions SignalR Service output binding reference.
Adds a user to a group and sends a message to the group:
Output:
