SignalR - Change server timeout response
Asked Answered
J

4

12

I've created a SignalR application but when I set the KeepAliveInternal and ClientTimeOutInterval a value in the hub configuration, the application ignores it and always sets it to "30,000ms" for both.

public void ConfigureServices(IServiceCollection services)
{
   services.AddRazorPages();
   services.AddSignalR().AddHubOptions<ActivityHub>(options => {
      options.ClientTimeoutInterval = TimeSpan.FromMinutes(30);
      options.KeepAliveInterval = TimeSpan.FromMinutes(15);
   });
}

I've read the SignalR Net Core docs and there is no limit for these two properties. The timeout always is "30,000" even though I set those to different values.

Jonette answered 3/1, 2020 at 22:44 Comment(1)
Are you using automatic reconnect on the client-side?Shute
T
9

when i set the KeepAliveInternal and ClientTimeOutInterval a value in the hub configuration, the application ignore it and always set to "30,000ms" for both.

For SignalR JavaScript client, the default serverTimeoutInMilliseconds value is 30,000 milliseconds (30 seconds). If you set KeepAliveInterval of HubOptions with a value > 30 seconds, but not specify an appropriate value for serverTimeoutInMilliseconds of HubConnection on client side, the connection will be terminated with an error, like below.

enter image description here

To fix it, you can try to set serverTimeoutInMilliseconds of your HubConnection, like below.

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub")
    .configureLogging(signalR.LogLevel.Trace)
    .build();

connection.serverTimeoutInMilliseconds = 120000;

Test Result

enter image description here

Note:

In my above test, I configure SignalR hubs with below code snippet, and we can find a ping message is sent automatically per 60s.

hubOptions.ClientTimeoutInterval = TimeSpan.FromMinutes(2);
hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
Tabu answered 6/1, 2020 at 5:7 Comment(2)
Where are you creating that new HubConnectionBuilder object?Pericope
@Pericope It can be created when you need it, but that already depends a lot on what you doJonette
A
5

Please refer to the official documentation for configuring server options

You may try to configure it as following:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR(hubOptions =>
    {
        hubOptions.ClientTimeoutInterval = TimeSpan.FromMinutes(30);
        hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(15);
    });
}

Or for a single hub:

services.AddSignalR().AddHubOptions<MyHub>(options =>
{
    options.ClientTimeoutInterval = TimeSpan.FromMinutes(30);
    options.KeepAliveInterval = TimeSpan.FromMinutes(15);
});
Anastasia answered 6/1, 2020 at 2:15 Comment(0)
S
3

I had the same problem back then, and what I changed was simple. I changed TimeSpan.FromMinutes to TimeSpan.FromSeconds since in the documentation you can see that those intervals are in seconds.

server config

So my configuration code now is like this:

/// <summary>
/// Adds SignalR.
/// </summary>
private void AddSignalR(IServiceCollection services)
{
    services.AddSignalR(hubOptions =>
    {
        hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(this.azureConfiguration.SignalR.ClientTimeoutInterval);
        hubOptions.HandshakeTimeout = TimeSpan.FromSeconds(this.azureConfiguration.SignalR.HandshakeTimeout);
        hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(this.azureConfiguration.SignalR.KeepAliveInterval);
        hubOptions.EnableDetailedErrors = this.azureConfiguration.SignalR.EnableDetailedErrors;
        hubOptions.MaximumReceiveMessageSize = this.azureConfiguration.SignalR.MaximumReceiveMessageSize;
        hubOptions.StreamBufferCapacity = this.azureConfiguration.SignalR.StreamBufferCapacity;
    }).AddAzureSignalR(azureOptions =>
    {
        azureOptions.ConnectionCount = this.azureConfiguration.SignalR.ServerConnectionCount;
    });
}
Sempach answered 6/1, 2020 at 15:17 Comment(1)
The returned object is always a TimeSpan, FromMintues and FromSeconds define the argument being passed into the factory method, and not what is returned.Inbreed
U
0

This is an answer concerning SignalR in Blazor-Apps.

As of now serverTimeoutInMilliseconds cannot be set in Startup.cs together with the other HubOptions. But you can solve the problem as follows:

.NET 6

In _Layout.cshtml after <script src="_framework/blazor.server.js" autostart="false"></script> add

<script>
    Blazor.start({
        configureSignalR: function (builder) {
            let c = builder.build();
            c.serverTimeoutInMilliseconds = 60000;
            c.keepAliveIntervalInMilliseconds = 30000;
            builder.build = () => {
                return c;
            };
        };
    });
</script>

(Source)

This will set serverTimeoutInMilliseconds to 60 seconds and the keepAliveInterval to 30 seconds.

.NET 8

In _Layout.cshtml after <script src="_framework/blazor.server.js" autostart="false"></script> add

<script>
    Blazor.start({
        configureSignalR: function (builder) {
            builder.withServerTimeout(60000).withKeepAliveInterval(30000);
        };
    });
</script>

(Source)

This will set serverTimeoutInMilliseconds to 60 seconds and the keepAliveInterval to 30 seconds.

Urba answered 19/12, 2023 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.