Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.0 Update: SignalR Config: stateful reconnect #30829

Closed
wants to merge 11 commits into from
57 changes: 56 additions & 1 deletion aspnetcore/signalr/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Learn how to configure ASP.NET Core SignalR apps.
monikerRange: '>= aspnetcore-2.1'
ms.author: bradyg
ms.custom: mvc
ms.date: 07/10/2023
ms.date: 10/23/2023
uid: signalr/configuration
---

Expand Down Expand Up @@ -325,6 +325,61 @@ var connection = new signalR.HubConnectionBuilder()

---

### Configure stateful reconnect

Stateful reconnect reduces the perceived downtime of clients that have a temporary disconnect in their network connection, such as when switching network connections or a short temporary loss in access. Stateful reconnect is available in ASP.NET Core 8.0 and later.

Stateful reconnect achieves this by:

* Temporarily buffering data on the server and client.
* Acknowledging messages received (ACK-ing) by both the server and client.
* Recognizing when a connection is returning and replaying messages that may have been sent while the connection was down.

To opt-in to stateful reconnect for a JavaScript or Typescript client:

* Update the JavaScript or TypeScript client code to enable the `withStatefulReconnect` option:

```JavaScript
const builder = new signalR.HubConnectionBuilder()
.withUrl("/hubname")
.withStatefulReconnect({ bufferSize: 1000 }); // Optional, defaults to 100,000
const connection = builder.build();
```

The `bufferSize` option is optional with a default of 100,000 bytes.

* Update the server hub endpoint configuration to enable the `AllowStatefulReconnects` option:

```csharp
app.MapHub<MyHub>("/hubName", options =>
{
options.AllowStatefulReconnects = true;
});
```

To opt-in to stateful reconnect for a .NET client:

* Update the .NET client code to enable the `WithStatefulReconnect` option:

```csharp
var builder = new HubConnectionBuilder()
.WithUrl("<hub url>")
.WithStatefulReconnect();
builder.Services.Configure<HubConnectionOptions>(o => o.StatefulReconnectBufferSize = 1000);
var hubConnection = builder.Build();
```

The `StatefulReconnectBufferSize` option is optional with a default of 100,000 bytes.

* Update the server hub endpoint configuration to enable the `AllowStatefulReconnects` option:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be listed first. Server must allow stateful reconnect otherwise client settings are ignored.

We also don't need to put it for both clients, if it's at the top that should be obvious 😄


```csharp
app.MapHub<MyHub>("/hubName", options =>
{
options.AllowStatefulReconnects = true;
});
```

### Configure additional options

Additional options can be configured in the `WithUrl` (`withUrl` in JavaScript) method on `HubConnectionBuilder` or on the various configuration APIs on the `HttpHubConnectionBuilder` in the Java client:
Expand Down