Search code examples
azureazure-functionssignalr-service

Send message to a group of users in SignalR Service in Azure Functions


Looking at the docs for the SignalR bindings to send a message to a specified user you include the UserId property on the message as such -

[FunctionName("SendMessage")]
public static Task SendMessage(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message, 
    [SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
    return signalRMessages.AddAsync(
        new SignalRMessage 
        {
            // the message will only be sent to these user IDs
            UserId = "userId1",
            Target = "newMessage", 
            Arguments = new [] { message } 
        });
}

This example is taken straight from the documentation, but the comment implies you message multiple userids, even though the property is a string and not an array.

How would you specify multiple users? (If for example, they are in a private chat channel together) Or is this mistake in the wording of the comment and you would need to send a message per user?

With other versions of SignalR I would put them in a group, but bindings for this do not exist for functions.


Solution

  • Unfortunately just like the doc says, right now with Azure function binding we can only send message to one user or to all clients.

    See the code of current extension SDK Microsoft.Azure.WebJobs.Extensions.SignalRService v1.0.0-preview1-10002. It shows the extension has only two methods SendToAll and SendToUser.

        Task SendToAll(string hubName, SignalRData data);
        Task SendToUser(string hubName, string userId, SignalRData data);
    

    The comment confused you is actually for old sample, the author forgot to modify it.

    Good news is that support for group operation is under progress.