csnewman / BlazorSignalR

SignalR Core C# client Blazor compatibility
MIT License
64 stars 21 forks source link

Obsolete

As of Blazor WebAssembly 3.2.0 Preview 1, the standard .NET SignalR client is now blazor compatible.

Please replace BlazorSignalR with Microsoft.AspNetCore.SignalR.Client and follow the standard configuration:

hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("/chatHub"))
            .Build();

BlazorSignalR Blazor=3.2.0-preview1 NuGet=BlazorSignalR

This package is a compatibility library for Microsoft ASP.NET Core SignalR to allow it to run on Microsoft ASP.NET Blazor.

The package is an addon for the existing .net client for SingalR, this is unlike the BlazorExtensions/SignalR package which emulates the c# api. This package instead works by replacing the transport mechanics, meaning the front facing SignalR api is still the standard .net one. The benefits of this setup is that as SignalR changes, this package takes little maintenance, as it only replaces the transport mechanisms, which are unlikely to change.

For more information about SignalR development, please check SignalR documentation.

Features

Install

Install the nuget package (or use the GUI in VS and search)

Install-Package BlazorSignalR

And then configure your connection creation like the following:

@inject IJSRuntime JsRuntime

HubConnection connection = new HubConnectionBuilder().WithUrlBlazor("/chathub", JsRuntime,
    options: opt => {
        opt.AccessTokenProvider = async () =>
        {
            return "some token for example";
        };
    }).Build();

Follow the official docs for the .NET core client.

Transports

All transports work

You will require browser support for WebSocket and EventSource for those transports to be enabled. You can install polyfils if you wish, as otherwise signalr will fallback to long polling.

You can manually select what transports and the implementations to use via Transports & Implementations in the BlazorHttpConnectionOptions when configuring the builder.

JS implemented means that the network requests are proxied to and from Javascript at a high level, whereas C# implemented means most of the processing occurs within the mono wasm runtime, with the low level networking being proxied back and forth. JS implementations should be faster as they use the underlying browser mechanisms.

Versions

Blazor BlazorSignalR
3.2.0-preview1 0.13.x
3.1.0-preview3 0.12.x
3.1.0-preview1 0.11.x
3.0.0-preview9 0.10.x
3.0.0-preview8 0.9.x
3.0.0-preview7 0.8.x
3.0.0-preview6 0.7.x
3.0.0-preview4 0.6.x
0.9.x 0.5.x
0.8.x 0.4.x
<= 0.7.x <= 0.3.x

The version of BlazorSignalR is tied lightly to the version of Blazor you are running. Generally the package is forwards compatible, however Blazor does have breaking changes once in a while, requiring a breaking BlazorSignalR version.

Alternatives

BlazorExtensions/SignalR

Uses the typescript client, exposed to C# via a shim api that relays back to typescript. This has the benefit of being extreemly reliable and fast, however at the expense of each feature needing to be hand exposed, meaning the api does not perfectly reflect the .net api.

This package uses the test suite from the BlazorExtensions/SignalR package and was based on their work. Please do check it out!