Search code examples
azureazure-functionsazure-signalr

Attempt to send message from Azure Function to Azure SignalR fails with 403 forbidden


I'm using Azure SignalR in serverless mode. I've an Azure Function which receives events from Event Hub and sends messages to Azure SignalR.

[Function(nameof(EventHubEventsHandler))]
[SignalROutput(HubName = "userevents", ConnectionStringSetting = "SignalRConnectionSetting")]
public async Task<List<SignalRMessageAction>> Run([EventHubTrigger("userschanged", Connection = "EventHubConnectionSetting")] EventData[] events)

{
    var messages = new List<SignalRMessageAction>();
    foreach (EventData @event in events)
    {
        try
        {
           messages.Add(new SignalRMessageAction("userChanged")
           {
               Arguments = new[] { "user changed" },
           });
        }
        catch (Exception ex)
        {
            ...
        }
    }
    return messages;
}

The connection string SignalRConnectionSetting in Azure Function App settings is defined like Endpoint=https://xxx.service.signalr.net;AccessKey=yyy;Version=1.0;

Azure Function is triggered successfully, but in Application Insights I see that the attempt to send message to Azure SignalR fails with 403 forbidden.

What I see in the logs:

GET https://xxx.service.signalr.net/api/v1/auth/accessKey
Result code: 200
POST https://xxx.service.signalr.net/api/hubs/userevents/:send?api-version=2022-06-01
Result code: 403
System.Net.Http.HttpRequestException
Exception while executing function: Functions.EventHubEventsHandler Azure SignalR service runtime error. Request Uri: https://xxx.service.signalr.net/api/hubs/userevents/:send?api-version=2022-06-01 Response status code does not indicate success: 403 (Forbidden) 

I've no clue what can be wrong.


Solution

    • Refresh the Signal R and Event Hub Connection strings and update the new values in the Application settings.
    • Make sure you have enabled Public Access in all the services.

    Follow below steps to achieve your requirement.

    • Created a EventHub Trigger Azure function to receive events from EventHub and send the messages to signal R.

    Code Snippet:

    [Function(nameof(EventHubEventsHandler))]
    [SignalROutput(HubName = "userevents", ConnectionStringSetting = "SignalRConnectionSetting")]
    public async Task<List<SignalRMessageAction>> Run([EventHubTrigger("kpeventhub", Connection = "EventHubConnectionString")] EventData[] events)
    {
        var messages = new List<SignalRMessageAction>();
        foreach (EventData @event in events)
        {
            try
            {
                messages.Add(new SignalRMessageAction("userChanged")
                {
                    Arguments = new[] { "user changed" },
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An error occurred while processing the event data.");
            }
        }
        return messages;
    }
    
    • Created a Signal R service and configured with Event Hub.
    • Enabled Application Insights in the Azure function app.
    • Added below Application Settings in the Function App=>Environment Variables:
    "EventHubConnectionString": "Endpoint=sb://<eventhubnamespace>.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=y8TjnnTJA2XXXXXX7+AEhJudoTs=",
    "SignalRConnectionSetting": "Endpoint=https://<signalr>.service.signalr.net;AccessKey=0vSNRXjzPnyQTQXXXXXnNZtC5pp8=;Version=1.0;"
    
    • Enable Live Trace in Signal R service=>Monitoring:

    enter image description here

    • Sending the Events:

    enter image description here

    Function App Invocations:

    enter image description here

    Live Trace:

    enter image description here

    • Logs in Application Insights:

    enter image description here