
SignalR enables real-time bi-directional communication between a Blazor WASM client and an ASP.NET Core server. Use it for live dashboards, chat, notifications, and collaborative features.
dotnet add package Microsoft.AspNetCore.SignalR.Client@implements IAsyncDisposable
@code {
private HubConnection? hubConnection;
protected override async Task OnInitializedAsync()
{
hubConnection = new HubConnectionBuilder()
.WithUrl(Nav.ToAbsoluteUri("/chathub"))
.WithAutomaticReconnect()
.Build();
hubConnection.On<string, string>("ReceiveMessage", (user, msg) =>
{
messages.Add($"{user}: {msg}");
InvokeAsync(StateHasChanged);
});
await hubConnection.StartAsync();
}
public async ValueTask DisposeAsync()
{
if (hubConnection is not null)
await hubConnection.DisposeAsync();
}
}InvokeAsync(StateHasChanged) from SignalR callbacks — they run on background threadsWithAutomaticReconnect() for resilienceIAsyncDisposableReference:
TaskLoco™ — The Sticky Note GOAT