How do you send multiple messages with the Azure SignalR Service output binding?

Adam Vincent 0 Reputation points
2024-12-14T18:39:06.8266667+00:00

The very first sentence of the doc suggest multiple messages can be sent with the SignalR Service output binding. Link: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-signalr-service-output?tabs=isolated-process%2Cnodejs-v4&pivots=programming-language-csharp

Use the SignalR output binding to send one or more messages

Can you please provide guidance on an appropriate approach to sending multiple messages with an output binding on a function?

I would like to see guidance added to the docs for multiple scenarios, but my specific case details would be:

  • Azure Functions v4 isolated
  • SignalR service configured in Serverless mode
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
151 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Adam Vincent 0 Reputation points
    2024-12-14T19:39:45.4333333+00:00

    Figured it out! Create a new POCO and move the output binding from the function to the new return type.

        [Function(nameof(MyFunction))]
        public async Task<SignalRMessages> MyFunction([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
        {
            HttpResponseData response = req.CreateResponse(HttpStatusCode.Accepted);
            SignalRMessageAction[] messages = [
                    new SignalRMessageAction(hubActionName, ["TEST1", "TEST1"]),
                    new SignalRMessageAction(hubActionName, ["TEST2", "TEST2"])
                    ];
            return await Task.FromResult(new SignalRMessages(messages));
        }
        public class SignalRMessages(IEnumerable<SignalRMessageAction> messages)
        {
            [SignalROutput(HubName = hubName, ConnectionStringSetting = appSettingSignalR)]
            public IEnumerable<SignalRMessageAction> Messages { get; set; } = messages;
        }
    

    More detailed answer in this SO answer.

    https://stackoverflow.com/a/79281253/7512742


  2. ajkuma 27,851 Reputation points Microsoft Employee
    2024-12-23T12:54:42.9833333+00:00

    Following-up from the comments, to benefit the community sharing the answer posted by Adam Vincent.

    Question/Issue: How do you send multiple messages with the Azure SignalR Service output binding?

    Azure doc ref:

    Use the SignalR output binding to send one or more messages using Azure SignalR Service.

    Copying the solution shared by Adam Vincent (Thank you!)

    Answer/solution:

    Create a new POCO and move the output binding from the function to the new return type.

    [Function(nameof(MyFunction))]
        public async Task<SignalRMessages> MyFunction([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
        {
            HttpResponseData response = req.CreateResponse(HttpStatusCode.Accepted);
            SignalRMessageAction[] messages = [
                    new SignalRMessageAction(hubActionName, ["TEST1", "TEST1"]),
                    new SignalRMessageAction(hubActionName, ["TEST2", "TEST2"])
                    ];
            return await Task.FromResult(new SignalRMessages(messages));
        }
        public class SignalRMessages(IEnumerable<SignalRMessageAction> messages)
        {
            [SignalROutput(HubName = hubName, ConnectionStringSetting = appSettingSignalR)]
            public IEnumerable<SignalRMessageAction> Messages { get; set; } = messages;
        }
    
    
    

    Linking Adam's SO post with a detailed answer ( with examples and different methods).


    Please click Accept Answer - it will benefit the community/users to find the answer quickly.

    0 comments No comments

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.