Search code examples
azuresignalrazure-signalr

Azure Function to SignalR (Serverless) to Web app


So I am rather stuck on this. We have been using "Default" SignalR with our Web apps for ages and all is fine.

We now want to add an Azure Function which requires SignalR to be Serverless. I can easily get SignalR and the Azure function to play ball.

However, I can not get the Web App to talk to SignalR as it says

"Critical: Service returned handshake error: Azure SignalR Service is in serverless mode, server connection is not allowed.. Id: fe6a4f57-6293-4b6f-bdec-9040ff60aef5"

Ok so looking at other tutorials like :

https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-quickstart-dotnet-core https://softchris.github.io/pages/dotnet-signalr.html#configure-signalr

You can do it in Javascript

const connection = new signalR.HubConnectionBuilder()
                .withUrl('https://*****-sb-staging-signalr.service.signalr.net/statushub')
                .build();
            bindConnectionMessage(connection);

However this gives me CORS issues, even though SignalR accepts all and I have in mty Startup.cs

services.AddCors(options =>
            {
                options.AddDefaultPolicy(builder =>
                {
                    builder.WithOrigins("https://******-sb-staging-admin.azurewebsites.net/")
                        .AllowCredentials();
                });
            });

and

services.AddSignalR(srConfig => srConfig.EnableDetailedErrors = true).AddAzureSignalR();
services.AddCors();

How on earth do I get a Web app to talk to SignalR Serverless


Solution

  • What's the role of your Azure Function? Do you still have Hub logics defined in your Web Apps?

    When in Default mode, your Web App handles the lifetime of the clients, the SignalR service acts like a proxy that it routes all the client traffics through server connections to the Web App, while in Serverless mode, the SignalR service handles the lifetime of the clients. This article describes the differences in detail.

    If you still have your Web App handle the clients in the Hub class, and Azure Function is simply used to send messages to these clients, please use Default mode.