ReactiveUI.Primitives extensions for Microsoft.AspNetCore.SignalR.Client.
Install-Package ReactiveUI.AspNetCore.SignalR.Client.RxUse the lean package above when the application does not otherwise depend on System.Reactive. Applications that require System.Reactive interoperability can install the shared-source variant:
Install-Package ReactiveUI.AspNetCore.SignalR.Client.Rx.ReactiveReactiveUI.AspNetCore.SignalR.Client.Rx provides high-performance, memory-efficient observable APIs for SignalR client applications. The lean package is based on ReactiveUI.Primitives; the .Reactive package uses ReactiveUI.Primitives.Reactive and the ReactiveUI.AspNetCore.SignalR.Client.Rx.Reactive namespace.
- Targets: .NET Framework 4.6.2, .NET 8, .NET 9, .NET 10, and .NET 11
- Supports: WPF, WinForms, Console, ASP.NET Core, Razor Pages, and more
using ReactiveUI.AspNetCore.SignalR.Client.Rx;
using Microsoft.AspNetCore.SignalR.Client;
var connection = new HubConnectionBuilder()
.WithUrl("http://localhost:5000/hub")
.WithAutomaticReconnect()
.Build();
// Subscribe to a hub method
var sub = connection.On(new HubMethod<string>("Stream")).Subscribe(Console.WriteLine);
// Start the connection reactively
connection.StartObservable().Subscribe();using ReactiveUI.AspNetCore.SignalR.Client.Rx;
HubBuilder.Create(builder => builder.WithUrl("https://localhost:53933/ChatHub"))
.Subscribe(x =>
{
var connection = x.HubConnection;
var disposables = x.Disposables;
// Listen for messages
disposables.Add(connection.On(new HubMethod<(string, string)>("ReceiveMessage"))
.Subscribe(msg => Console.WriteLine($"{msg.Value1}: {msg.Value2}")));
// Handle connection closed
disposables.Add(connection.HasClosed().Subscribe(error =>
Console.WriteLine($"Closed: {error?.Message}")));
// Start the connection from any observable trigger.
// disposables.Add(connectRequested.Start(connection).Subscribe());
});connection.StreamObservable(new HubMethod<int>("Counter"), CancellationToken.None, 10)
.Subscribe(
value => Console.WriteLine($"Received: {value}"),
ex => Console.WriteLine($"Error: {ex.Message}"),
() => Console.WriteLine("Stream completed"));// Invoke a method and get a result
connection.InvokeObservable(new HubMethod<int>("AddNumbers"), CancellationToken.None, 1, 2)
.Subscribe(result => Console.WriteLine($"Sum: {result}"));
// Send a method (fire-and-forget)
connection.SendObservable("Notify", CancellationToken.None, "Hello!")
.Subscribe(_ => Console.WriteLine("Notification sent"));connection.StateChanges().Subscribe(state =>
Console.WriteLine($"Connection state: {state}"));
// Wait for a specific state
connection.WaitForState(HubConnectionState.Connected)
.Subscribe(_ => Console.WriteLine("Connected!"));@page
@using ReactiveUI.AspNetCore.SignalR.Client.Rx
@inject Microsoft.AspNetCore.SignalR.Client.HubConnection Connection
@functions {
protected override void OnInitialized()
{
Connection.On(new HubMethod<string>("ReceiveMessage"))
.Subscribe(msg => /* update UI */);
Connection.StartObservable().Subscribe();
}
}On(string methodName)— No-arg handlerOn<T>(HubMethod<T> method)On<T1, T2>(HubMethod<(T1, T2)> method)- ... up to 8 arguments
StartObservable()/StartObservable(CancellationToken)StartObservable(int? retryCount, CancellationToken)StopObservable()/StopObservable(CancellationToken)StopObservable(int? retryCount, CancellationToken)EnsureStarted()/EnsureStarted(CancellationToken)StateChanges()WaitForState(HubConnectionState)
StreamObservable<T>(HubMethod<T> method, CancellationToken, params object?[] args)
InvokeObservable<T>(HubMethod<T> method, CancellationToken, params object?[] args)InvokeObservable(string methodName, CancellationToken, params object?[] args)SendObservable(string methodName, CancellationToken, params object?[] args)
HasClosed()IsReconnecting()HasReconnected()
HubBuilder.Create(Func<HubConnectionBuilder, IHubConnectionBuilder>)
- Always dispose subscriptions to avoid memory leaks.
- Use the disposable scope returned by
HubBuilder.Createfor managing multiple subscriptions. - Prefer
StartObservable/StopObservablefor connection management in Rx scenarios. - Use
StreamObservablefor server-to-client streaming.
MIT
ReactiveUI.AspNetCore.SignalR.Client.Rx - Empowering Industrial Automation with Reactive Technology ⚡🏭