MT.Blazor.ConnectivityDetection 1.0.0

dotnet add package MT.Blazor.ConnectivityDetection --version 1.0.0
NuGet\Install-Package MT.Blazor.ConnectivityDetection -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MT.Blazor.ConnectivityDetection" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MT.Blazor.ConnectivityDetection --version 1.0.0
#r "nuget: MT.Blazor.ConnectivityDetection, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MT.Blazor.ConnectivityDetection as a Cake Addin
#addin nuget:?package=MT.Blazor.ConnectivityDetection&version=1.0.0

// Install MT.Blazor.ConnectivityDetection as a Cake Tool
#tool nuget:?package=MT.Blazor.ConnectivityDetection&version=1.0.0

MT.Blazor.ConnectivityDetection

Blazor WASM component library for network connectivity detection

Nuget

Installing

You can install from Nuget using the following command:

Install-Package MT.Blazor.ConnectivityDetection

Or via the Visual Studio package manger.

Basic Usage

Start by add the following using statement to your root _Imports.razor.

@using MT.Blazor.ConnectivityDetection

Then add the connectivity state provider in the App.razor root component around the Router.

<CascadingConnectivityState>
    <Router AppAssembly="@typeof(Program).Assembly">
        <Found Context="routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        </Found>
        <NotFound>
            <p>Sorry, there's nothing at this address.</p>
        </NotFound>
    </Router>
</CascadingConnectivityState>

The CascadingConnectivityState component subscribes to and starts the network connectivity detection by the IConnectivityStateProvider upon initialization and notifies nested components of change in network connectivity with ConnectivityState.

The IConnectivityStateProvider is the underlying service that subscribes to the javascript based network connectivity detection and provides ConnectivityState to subscribers.

Registering Connectivity Detection

Register and configure connectivity detection in IServiceCollection in Program.cs

builder.Services.AddConnectivityDetection();

By default, network connectivity state is determine with the browser's navigator.onLine information and quality of connection with the Network Information API.

Since the Network Information API is experimental and has varying browser compatibility, it is highly recommended to configure a HTTP ping check with the ConnectivityOptions in the AddConnectivityDetection method.

builder.Services.AddConnectivityDetection(options => 
{
    options.PingUrl = "/api/ping";
    options.PingInterval = 1000;
});

These options will start an HTTP based connectivity check in addition to the Network Information API detection every second. You can configure any API end point to use as a ping check, however, it's best to host the end point in the server for an ASP.NET Core hosted Blazor WebAssembly application to have the connectivity state be based on latency between with the hosting server application.

Latency

When performing ping checks, latency is calculated in milliseconds to completion of a successfully HTTP GET to the end point.

With the Network Information API based connectivity detection, due to browser compatibility and the experimental nature of the API, the latency value is a static, representative value in milliseconds as follows:

  • effectiveType value 4g and downlink value of 10 for a fast connection is represented as 50ms
    • downlink value greater than or equal to 5 is represented as 500ms and 1000ms otherwise
  • effectiveType value 3g is represented as 500ms and 1000ms otherwise

When a ping check is configured, then an ad-hoc ping check is performed when the latency measurement as described previously is greater than 50ms as an accurate verification.

Connectivity Status

The ConnectivityState value that cascades from the CascadingConnectivityState as provided by the IConnectivityStateProvider conveys the connectivity check type, latency, and status to the client application as ConnectivityStatus enum value of Offline, Online, or Slow.

The thresholds for the the statuses as as follows:

  • Offline when navigator.onLine is false or the ping check fails
  • Online when latency value is less than 500ms otherwise Slow
    • The 500ms threshold currently is not configurable

Any service can retrieve the current ConnectivityState by injecting the IConnectivityStateProvider as a dependency and query the current ConnectivityState by calling the IConnectivityStateProvider.GetConnectivityStateAsync() method or by subscribing to the IConnectivityStateProvider.ConnectivityStateChanged event.

Change Log

  • 0.0.0 Pre-release
  • 1.0.0 Initial release

Github Repository | Nuget Package

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 609 8/20/2022
0.0.0 380 8/19/2022