In ASP.NET Core SignalR, how do KeepAliveInterval, ClientTimeoutInterval and the client's server timeout fit together, and what else makes the server close a connection?
answer
- 15 s pings, 30 s timeouts
- timeout at least twice the other side's keep-alive
- change one side, change the other
- MaximumReceiveMessageSize 32 KB closes the connection
- HandshakeTimeout and TransportSendTimeout
basics
~20 sOn WebSockets and Server-Sent Events each side pings when idle (every 15 s) and gives up after 30 s of silence: ClientTimeoutInterval on the server, the server timeout on the client, each at least double the other's keep-alive. An incoming message over 32 KB also closes the connection.
solid answer
~50 sThe server sends a ping when it has sent nothing for `HubOptions.KeepAliveInterval` (15 s); the client closes the connection when it hears nothing for its server timeout, 30 s by default (`withServerTimeout` in JavaScript). The client pings at its own keep-alive interval (15 s), and the server closes the connection when the client is silent for `ClientTimeoutInterval` (30 s). The rule is that each timeout is at least double the other side's keep-alive, so raising the server's `KeepAliveInterval` to 60 s without raising the client's server timeout to 120 s disconnects every idle client. Other server-side limits close a connection as well: `HandshakeTimeout` (15 s) for the opening handshake; `MaximumReceiveMessageSize` (32 KB), where one larger incoming message closes the whole connection with "Connection closed with an error."; and `TransportSendTimeout` (10 s) for a single send to a slow client. Long polling is the exception to the ping rules: it sends no pings, and each poll is held for up to `LongPollingOptions.PollTimeout` (90 s).
code
csharp · 9 linesbuilder.Services.AddSignalR(o =>
{
o.KeepAliveInterval = TimeSpan.FromSeconds(30);
o.ClientTimeoutInterval = TimeSpan.FromSeconds(60);
})
.AddHubOptions<QuotesHub>(o =>
{
o.MaximumReceiveMessageSize = 64 * 1024; // this hub accepts larger order tickets
});go deeper
Recall the defaults: pings every 15 seconds, a 30-second timeout on each side, and a 32 KB limit on incoming hub messages.
Explain which side owns each setting, the two-to-one rule between a timeout and the other side's keep-alive, and that an oversized message closes the connection.
Tune these as a pair across server and deployed clients, relate them to proxy idle limits, and keep large payloads off the hub instead of raising limits blindly.
Balance detection speed for dead clients against ping traffic and buffer memory per connection, and plan how setting changes roll out to clients you do not control.
## Two keep-alives, two timeouts A SignalR connection can sit idle for minutes, so each side proves it is alive with **ping** messages and gives up on the other after a silent window: | Setting | Side | Default | Meaning | |---|---|---|---| | `HubOptions.KeepAliveInterval` | server | 15 s | send a ping if nothing else was sent this long | | `HubOptions.ClientTimeoutInterval` | server | 30 s | close if the client sent nothing this long | | `withKeepAliveInterval` / `KeepAliveInterval` | client | 15 s | send a ping if nothing else was sent this long | | `withServerTimeout` / `ServerTimeout` | client | 30 s | close if the server sent nothing this long | | `HubOptions.HandshakeTimeout` | server | 15 s | the new connection must send its handshake within this window | Any message resets the timers; pings only fill silence. The JavaScript client exposes the client settings on `HubConnectionBuilder` as `withServerTimeout(ms)` and `withKeepAliveInterval(ms)`, and the .NET client has `WithServerTimeout` and `WithKeepAliveInterval`. ## The two-to-one rule Each timeout should be **at least twice** the other side's keep-alive interval, leaving room for one ping to be late: - server `KeepAliveInterval` 15 s, client server timeout 30 s: fine; - server `KeepAliveInterval` raised to 60 s, client server timeout still 30 s: every idle client closes after 30 s with "Server timeout elapsed without receiving a message from the server."; - client keep-alive raised to 30 s, server `ClientTimeoutInterval` still 30 s: the server closes idle clients with "Client hasn't sent a message/ping within the configured ClientTimeoutInterval." So change both ends together, and remember that clients already deployed keep their old values until they update. ## Proxies and idle connections The pings are also what keep an idle connection open through proxies and load balancers that drop silent connections. A corporate proxy with a 60-second idle limit is satisfied by the default 15-second pings; stretch the interval past the proxy's limit and connections start dropping in the quiet minutes. Long polling is the exception. SignalR treats it as inherently kept alive, so neither side sends pings on it and these timeouts do not drive it; instead the server holds an empty poll for up to `LongPollingOptions.PollTimeout`, 90 s by default, and a proxy that cuts requests sooner breaks the polls. One more detail from the server source: it starts enforcing `ClientTimeoutInterval` only after the client's first ping, so clients that never ping are not timed out this way. ## Message size closes the connection, not the call `HubOptions.MaximumReceiveMessageSize` caps a single **incoming** hub message at 32 KB by default. When a client sends more: 1. the server's parser hits the limit and throws `InvalidDataException`, "The maximum message size of 32768B was exceeded. The message size can be configured in AddHubOptions."; 2. the server ends the connection and sends a close message, "Connection closed with an error."; 3. the JavaScript client's `onclose` receives "Server returned an error on close: Connection closed with an error." The whole connection is lost, not just that invocation. Raise the limit for one hub with `AddHubOptions<THub>`, or globally in `AddSignalR(o => ...)`, knowing that every connection may then make the server buffer that much. For large payloads, upload over an ordinary HTTP endpoint and send a reference, or stream the data in chunks. ## Slow clients `HttpConnectionDispatcherOptions.TransportSendTimeout`, 10 s by default and set in the `MapHub` options, closes a connection when a single send to the client takes longer, protecting the server from clients that stop reading.
- Why does a client that sends a 40 KB message lose its whole connection instead of getting an error for that call?The size check happens while the server parses the incoming byte stream, before it knows which invocation the bytes belong to. It throws `InvalidDataException`, and the only safe response to an unparseable stream is to close the connection, which the client sees as "Connection closed with an error." Raise `MaximumReceiveMessageSize` or send large data another way.
- Does MaximumReceiveMessageSize limit what the server sends to clients?No. It applies to messages the server receives. Outgoing data is governed by the transport's buffers, `TransportMaxBufferSize` at 64 KB by default, which apply backpressure rather than reject a message, and by `TransportSendTimeout`, which closes a connection when one send stalls for 10 seconds.
- Raising KeepAliveInterval on the server is enough to keep idle SignalR connections open longer.
- A message over MaximumReceiveMessageSize fails only that hub invocation.
- ClientTimeoutInterval is how often the server pings the client.
- The client's server timeout should equal the server's keep-alive interval.
- MaximumReceiveMessageSize also caps messages the server sends to clients.