skip to content

What does SignalR stateful reconnect add over withAutomaticReconnect(), and what must the server and the client each enable to use it?

level: seniorimportance: should knowfreq 30%

answer

  1. opt-in on both ends since .NET 8
  2. AllowStatefulReconnects on MapHub options
  3. withStatefulReconnect / WithStatefulReconnect()
  4. buffer, ack, replay: 100,000 bytes
  5. WebSockets transport only

basics

~20 s

Stateful reconnect, opt-in since .NET 8, lets a briefly dropped WebSocket reattach to the same connection: both sides buffer and acknowledge messages and replay what was missed, keeping the connection id. Enable AllowStatefulReconnects on MapHub and withStatefulReconnect() (WithStatefulReconnect() in .NET) on the client.

solid answer

~50 s

Plain automatic reconnect opens a brand-new connection: new connection id, lost server-side state, rejected pending calls and no record of pushes sent meanwhile. Stateful reconnect, added in ASP.NET Core 8, makes both sides keep a buffer of sent messages, acknowledge what they received, and after a short WebSocket drop reattach to the same connection and replay the unacknowledged messages, so the connection id, its groups and in-flight calls survive. It must be enabled at both ends: on the server `app.MapHub<TickerHub>("/ticker", o => o.AllowStatefulReconnects = true)`, and on the client `.withStatefulReconnect()` in JavaScript or `.WithStatefulReconnect()` in .NET. Buffers default to 100,000 bytes per connection: `HubOptions.StatefulReconnectBufferSize` on the server, `bufferSize` in JavaScript, `HubConnectionOptions.StatefulReconnectBufferSize` in .NET; a full buffer applies backpressure to sends. It only works over WebSockets, and if the reattach fails the connection closes and ordinary automatic reconnect, if configured, takes over.

code

csharp · 13 lines
csharp
// Server: Program.cs
builder.Services.AddSignalR();
var app = builder.Build();
app.MapHub<TickerHub>("/ticker", o => o.AllowStatefulReconnects = true);

// .NET kiosk client
var clientBuilder = new HubConnectionBuilder()
    .WithUrl("https://ticker.example.com/ticker")
    .WithStatefulReconnect()        // reattach and replay on a short drop
    .WithAutomaticReconnect();      // fall back to a new connection on a long one
clientBuilder.Services.Configure<HubConnectionOptions>(
    o => o.StatefulReconnectBufferSize = 200_000);
var connection = clientBuilder.Build();

go deeper

for a junior

Know that stateful reconnect exists since .NET 8, keeps the same connection after a short drop, and must be switched on at both ends.

for a middle

Explain buffering, acknowledgement and replay, the AllowStatefulReconnects and withStatefulReconnect settings, and the 100,000-byte buffer defaults.

for a senior

State its limits (WebSockets only, short drops, in-memory buffers) and combine it with automatic reconnect and an onreconnected fallback.

for a principal

Weigh per-connection buffer memory against the user experience of brief network switches across the fleet of clients you expect.

## The gap it closes `withAutomaticReconnect()` recovers from a drop by **starting over**. The server sees a new connection with a new id, anything tied to the old id is gone, pending invocations were rejected, and messages pushed during the outage never reach the client. For a phone switching from Wi-Fi to mobile data for two seconds, that is a lot of disruption. **Stateful reconnect**, added in ASP.NET Core 8 and unchanged in 10, reduces that perceived downtime by: - temporarily **buffering** sent data on the server and the client; - **acknowledging** received messages in both directions; - after the transport is back, **replaying** the messages the other side had not acknowledged. Because the client reattaches to the **same** logical connection, the connection id is kept, so server-side state such as group membership and `Context.Items` survives, and calls in flight can complete. ## Enabling it: both ends It is opt-in on the server endpoint **and** on the client; one side alone does nothing. ```csharp // Server: per hub endpoint (HttpConnectionDispatcherOptions) app.MapHub<TickerHub>("/ticker", options => { options.AllowStatefulReconnects = true; // default false }); // Optional server buffer, all hubs or one hub builder.Services.AddSignalR(o => o.StatefulReconnectBufferSize = 200_000); ``` ```javascript // JavaScript client const connection = new signalR.HubConnectionBuilder() .withUrl("/ticker") .withStatefulReconnect({ bufferSize: 200000 }) // optional, default 100,000 .build(); ``` The .NET client calls `.WithStatefulReconnect()` on `HubConnectionBuilder` and sets the size with `builder.Services.Configure<HubConnectionOptions>(o => o.StatefulReconnectBufferSize = ...)`. ## The settings | Setting | Where | Default | |---|---|---| | `AllowStatefulReconnects` | `HttpConnectionDispatcherOptions`, via `MapHub` | `false` | | `StatefulReconnectBufferSize` | `HubOptions` (server) | 100,000 bytes | | `bufferSize` | `withStatefulReconnect(options)` (JavaScript) | 100,000 bytes | | `StatefulReconnectBufferSize` | `HubConnectionOptions` (.NET client) | 100,000 bytes | The buffer bounds memory per connection. When unacknowledged data reaches it, further sends wait (backpressure) until acknowledgements free space, so a slow or absent peer slows the sender rather than growing memory without limit. ## Limits worth stating in an interview 1. **WebSockets only.** The clients turn the feature on only when the negotiated transport is WebSockets; over Server-Sent Events or long polling it does not apply. 2. **Short drops.** It is designed for temporary interruptions. If the reattach fails, the connection closes as usual. 3. **It complements automatic reconnect.** Configure both: stateful reconnect handles the brief blip invisibly, and `withAutomaticReconnect()` handles a longer outage by opening a new connection, with the usual consequences of a new id. 4. **No persistence.** Buffers live in memory on the two ends; a server restart still ends the connection. 5. **Version requirement.** It uses version 2 of the hub protocol handshake, so both ends must be ASP.NET Core 8 or later. The Azure SignalR Service SDK supports it from v1.26.1. ## What the app still owns Even with stateful reconnect, design for the fallback path: a long outage still produces a brand-new connection, so the ticker dashboard keeps its `onreconnected` handler that rejoins and refetches. Stateful reconnect improves the common case; it does not remove the need for the rare one. ## A note on events In the JavaScript client, the reattach happens in the transport layer beneath `HubConnection`: the WebSocket reconnects and the buffered messages are resent, without the `HubConnection` passing through its `Reconnecting` state. The `onreconnecting` and `onreconnected` callbacks belong to the full automatic-reconnect path.

  • The client enabled withStatefulReconnect() but the server endpoint did not set AllowStatefulReconnects. What happens on a drop?
    Nothing stateful. The server does not offer the feature during negotiation, so the connection behaves as before: a drop closes it, and only `withAutomaticReconnect()`, if configured, recovers by opening a new connection with a new id.
  • Why does a large StatefulReconnectBufferSize deserve a second thought on a busy server?
    The buffer is per connection and in memory. Ten thousand connections each allowed hundreds of kilobytes of unacknowledged data is gigabytes in the worst case. Size it for the burst you need to survive a short blip; a full buffer applies backpressure to sends rather than dropping messages.

  • Stateful reconnect is on by default since .NET 8.
  • Enabling withStatefulReconnect() on the client alone is enough.
  • Stateful reconnect works over long polling and Server-Sent Events too.
  • With stateful reconnect the client gets a new connection id after a blip.
  • Stateful reconnect makes withAutomaticReconnect() unnecessary.

Assumes ASP.NET Core SignalR 10 and its clients; stateful reconnect was introduced in ASP.NET Core 8 as opt-in on both ends, and the 10.0 release notes list no SignalR change to it.