top of page

Connection Management in SignalR: ServerTimeout and KeepAliveInterval Settings

SignalR, a real-time web application framework, relies on effective connection management to ensure seamless communication between clients and servers. Key to this management are the ServerTimeout and KeepAliveInterval settings.


In this article, we'll explore the significance of these settings and their evolution in newer SignalR versions, allowing you to create a tailored connection management strategy for your real-time applications.


Table of Contents:

SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the ability to have server-side code push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data.


What is Connection Management in SignalR?

Connection management in SignalR is a critical aspect of maintaining real-time web functionality. It involves managing the lifecycle of connections between the server and the client, which includes handling the connection, reconnection, and disconnection events.


Here are some key aspects of connection management in SignalR:

  • Connection Lifetime: SignalR provides events that you can handle to manage the lifecycle of a connection. These include events for when a connection is established (OnConnected), when a connection is terminated (OnDisconnected), and when a connection is re-established after being lost (OnReconnected).

  • Timeout and KeepAlive Settings: SignalR allows you to configure timeout and keepalive settings. The ServerTimeout setting determines how long the server waits for a message from the client before considering the connection disconnected. The KeepAliveInterval setting specifies the maximum amount of time that can elapse without receiving any data from the server.

  • Handling Disconnections: SignalR provides mechanisms to handle disconnections, whether they are caused by clients disconnecting, servers going down, or network issues.

  • Managing Multiple Connections: When using SignalR, one of the most important things will be to manage the various connections made by the clients, as well as being able to send messages to specific clients.


Understanding ServerTimeout and KeepAliveInterval


ServerTimeout: This setting is used to determine how long the server should wait for a message from the client before considering the connection disconnected. The default value is 30 seconds.


If the server doesn’t receive any message from the client within 30 seconds, it will consider the connection as disconnected. This timeout helps in detecting and handling situations where the client is no longer available or has lost connectivity.


For the SignalR JavaScript client, the default serverTimeoutInMilliseconds value is 30,000 milliseconds (30 seconds).


KeepAliveInterval: This setting is used to specify the maximum amount of time that can elapse without any data being received from the server. If no data is received within this interval, a ping message is sent to ensure the connection is active. The default value is 15 seconds.


If no data is received from the server within 15 seconds, a ping message will be sent to keep the connection alive.


Older Version

In earlier versions of SignalR (ASP.NET Core 7.0 or earlier), the ServerTimeout and KeepAliveInterval settings were set directly on the connection object after it was built.


Here’s an example:

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

connection.serverTimeoutInMilliseconds = 60000; 
// ServerTimeout set to 60 seconds

connection.keepAliveIntervalInMilliseconds = 30000; 
// KeepAliveInterval set to 30 seconds

While this approach worked, it had a couple of potential issues:

  1. Less Intuitive: This approach required developers to first build the connection and then modify its properties. This could be less intuitive as developers usually expect to configure all settings before building an object.

  2. Error-Prone: Directly modifying the properties of the connection object after it’s built could potentially lead to errors, especially if these properties are modified elsewhere in the code.

The New Version

The new approach introduced in ASP.NET Core 8.0 addresses these issues by allowing developers to configure these settings directly on the HubConnectionBuilder, resulting in a more fluent and intuitive API.


What is HubConnectionBuilder?

The HubConnectionBuilder is a class in SignalR that is used to build a connection to a SignalR hub. It is used to configure the connection to the hub, including the URL of the hub, the transport protocol to use, and the options for automatic reconnection.


For instance, you can establish a connection by creating a HubConnectionBuilder and calling Build. The hub URL, protocol, transport type, log level, headers, and other options can be configured while building a connection.


Here’s how you can set the ServerTimeout and KeepAliveInterval settings using the new approach in ASP.NET Core 8.0 or later:

var connection = new signalR.HubConnectionBuilder()
  .withUrl("/chatHub")
  
  .withServerTimeoutInMilliseconds(60000) 
  // ServerTimeout set to 60 seconds
  
  .withKeepAliveIntervalInMilliseconds(30000) 
  // KeepAliveInterval set to 30 seconds
  
  .build();

In this example, the withServerTimeoutInMilliseconds and withKeepAliveIntervalInMilliseconds methods are used to set the ServerTimeout and KeepAliveInterval settings respectively, before the connection is built.


This provides a more fluent and intuitive way to configure these settings. It also helps prevent potential errors that could occur when modifying these properties after the connection is built.


Customizing ServerTimeout and KeepAliveInterval

In SignalR, you can customize the ServerTimeout and KeepAliveInterval settings directly on the HubConnectionBuilder. This allows you to adjust these settings based on the specific needs of your application.


Here’s how you can set custom values for these settings:

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

// Set ServerTimeout to 60 seconds
connection.serverTimeoutInMilliseconds = 60000;

// Set KeepAliveInterval to 30 seconds
connection.keepAliveIntervalInMilliseconds = 30000;

In this example, serverTimeoutInMilliseconds is set to 60000 milliseconds (or 60 seconds), and keepAliveIntervalInMilliseconds is set to 30000 milliseconds (or 30 seconds).


Impact of Custom Settings

Customizing the ServerTimeout and KeepAliveInterval settings can significantly impact the performance and reliability of your SignalR applications. Here’s how:


Performance: Adjusting these settings can help optimize the performance of your application. For example, increasing the ServerTimeout can be beneficial in scenarios where clients have unreliable network connections and need more time to send messages to the server. On the other hand, decreasing the KeepAliveInterval can help in quickly detecting disconnected clients and freeing up resources.


Reliability: These settings also play a crucial role in maintaining a reliable connection between the server and client. A shorter KeepAliveInterval ensures that the server frequently checks the status of the client, which can lead to quicker detection of any issues with the connection.


However, there are potential issues to consider when customizing these settings:

  • Resource Usage: Decreasing the KeepAliveInterval means that keep-alive messages are sent more frequently, which can increase network traffic and CPU usage on both the server and client.

  • Connection Stability: Increasing the ServerTimeout means that the server waits longer for messages from the client before considering the connection disconnected. This could potentially lead to situations where disconnected clients are not detected quickly, which could impact the performance of your application.


Conclusion

You've learned the significance of these settings and how they have evolved in newer versions of SignalR to enhance connection stability. Customizing these settings allows you to tailor connection management to your application's unique needs.

bottom of page