Isolated worker process to send message with SignalR

Sreeram Jayaram 0 Reputation points
2024-01-05T10:29:29.09+00:00
[Function(SendMessageToUserFunctionName)]
        [SignalROutput(HubName = "SignalRHub", ConnectionStringSetting = "AzureSignalRConnectionString")]
        public async Task<SignalRMessageAction> SendMessageToUserAsync(
               [ServiceBusTrigger("queuename", Connection = "ServiceBusConnectionString")] ServiceBusReceivedMessage message,
               [SignalRConnectionInfoInput(HubName = "SignalRHub", ConnectionStringSetting = "AzureSignalRConnectionString")] IAsyncCollector<SignalRMessageAction> signalRMessages,
               ILogger log)
        {
            try
            {
               //.. custom logic code

                    var messageArgument = JsonConvert.SerializeObject(new
                    {
                        pushNotificationMessage.Message,
                        pushNotificationMessage.ResponseEndpoint,
                    });

                    return new SignalRMessageAction("SignalRHub", new[] { messageArgument })
                    {
                        UserId = encodedUserId,
                    };
                
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, ex.Message);
                throw;
            }
        }

I've developed a function to send messages to specific users using SignalR in an isolated worker process. I'd like to confirm if this approach is correct for sending messages in an isolated worker process and if the parameters are appropriately configured. Additionally, I'm considering using 'IAsyncCollector' in this context. Can you provide guidance on whether this is suitable for an isolated worker process?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,070 questions
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
146 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael John Pena 165 Reputation points MVP
    2024-01-05T11:16:24.21+00:00

    Your approach to sending messages using SignalR in an isolated worker process seems correct. You’re using the ServiceBusTrigger to trigger the function when a message arrives in the Service Bus queue, which is a common pattern for asynchronous messaging.

    The SignalROutput attribute is used to send messages to a SignalR hub. The HubName and ConnectionStringSetting parameters seem to be correctly configured, assuming that “SignalRHub” is the name of your SignalR hub and “AzureSignalRConnectionString” is the name of the app setting that contains your Azure SignalR Service connection string.

    The IAsyncCollector<SignalRMessageAction> parameter, signalRMessages, is used to send multiple output items (in this case, SignalR messages) from your function. This is suitable for an isolated worker process. The IAsyncCollector interface provides an AddAsync method that you can call to add items to the output collection. These items are sent when your function completes.

    However, please ensure that the UserId is correctly encoded and the pushNotificationMessage.Message and pushNotificationMessage.ResponseEndpoint are correctly populated.

    Things to explore:

    • logging to trace the issue
    • retry if functions logs an exception
    • dead-letter queues
    • try testing different message sizes
    • test when SignalR is offline

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.