Smiranin
Smiranin

Reputation: 746

How to configure KeepAliveInterval HubOptions for Azure SignalR Serverless?

Is there a way to configure the KeepAliveInterval option for a SignalR hub when using Azure SignalR Service?

I'm primarily a frontend developer, so I'm not very familiar with the backend setup. We are using Azure SignalR Service with a ServerlessHub in an in-process model, as far as I understand. You can see the relevant documentation

I've gone through the available docs but haven't found a way to configure this option. A backend developer mentioned that it should be added to our local.settings.json. Currently, our configuration looks something like this:

{
  "values": {
     "Azure:SignalR:HubProtocol:NewtonsoftJson:CamelCase": true,
   }
}

Upvotes: 1

Views: 117

Answers (2)

Blit
Blit

Reputation: 55

This was previously not supported for serverless signalR, but it is now. Related github issue

It can be set in the azure portal of the SignalR resource under 'Settings': enter image description here

Upvotes: 0

Pravallika KV
Pravallika KV

Reputation: 8579

Configure KeepAliveInterval to set the interval used by the server using AddSignalR method in Startup.cs file to configure SignalR service options.

Set the KeepAliveProperty in Startup.cs:

options.KeepAliveInterval = TimeSpan.FromSeconds(120);

I have created a .NET in-process Signal Azure function and used below code to configure KeepAliveInterval in Startup.cs.

Startup.cs:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using Microsoft.Azure.SignalR;
using System;
using System.Threading.Tasks;

[assembly: FunctionsStartup(typeof(FunctionApp35.Startup))]

namespace FunctionApp35
{ 
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSignalR(options =>
                {
                    // Set the KeepAliveInterval
                    options.KeepAliveInterval = TimeSpan.FromSeconds(120);
                });
        }
    }
}

Function.cs:

   [FunctionName("negotiate")]
   public static SignalRConnectionInfo Negotiate(
       [HttpTrigger(AuthorizationLevel.Anonymous, "get","post")] HttpRequest req,
       [SignalRConnectionInfo(HubName = "eventhub")] SignalRConnectionInfo connectionInfo)
   {
       return connectionInfo;
   }

Console Response:

Functions:

        negotiate: [GET,POST] http://localhost:7003/api/negotiate

For detailed output, run func with --verbose flag.
[2024-09-17T12:46:35.657Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-09-17T12:46:42.135Z] Executing 'negotiate' (Reason='This function was programmatically called via the host APIs.', Id=f23d5ee9-8XXX9bd-a108-005c4d235fa3)
[2024-09-17T12:46:42.205Z] Executed 'negotiate' (Succeeded, Id=f23d5eeXXX8-49bd-a108-005c4d235fa3, Duration=245ms)

enter image description here

Upvotes: 0

Related Questions