Skip to content

BlazorExtensions/SignalR

Repository files navigation

Build Package Version NuGet Downloads License

DEPRECATION NOTE: This package is no longer required since Blazor WebAssembly now supports SignalR Client. Users of this package should stop using it and use the official client instead.

Blazor Extensions

Blazor Extensions is a set of packages with the goal of adding useful features to Blazor.

Blazor Extensions SignalR

This package adds a Microsoft ASP.NET Core SignalR client library for Microsoft ASP.NET Blazor.

The package aims to mimic the C# APIs of SignalR Client as much as possible and it is developed by wrapping the TypeScript client by using Blazor's interop capabilities.

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

Features

This package implements all public features of SignalR Typescript client.

Note: The Streaming APIs are not implemented yet. We will add it soon.

Sample usage

The following snippet shows how to setup the client to send and receive messages using SignalR.

The HubConnectionBuilder needs to get injected, which must be registered:

// in Startup.cs, ConfigureServices()
   services.AddTransient<HubConnectionBuilder>();
// in Component class
[Inject]
private HubConnectionBuilder _hubConnectionBuilder { get; set; }
// in Component Initialization code
var connection = _hubConnectionBuilder // the injected one from above.
        .WithUrl("/myHub", // The hub URL. If the Hub is hosted on the server where the blazor is hosted, you can just use the relative path.
        opt =>
        {
            opt.LogLevel = SignalRLogLevel.Trace; // Client log level
            opt.Transport = HttpTransportType.WebSockets; // Which transport you want to use for this connection
        })
        .Build(); // Build the HubConnection

connection.On("Receive", this.Handle); // Subscribe to messages sent from the Hub to the "Receive" method by passing a handle (Func<object, Task>) to process messages.
await connection.StartAsync(); // Start the connection.

await connection.InvokeAsync("ServerMethod", param1, param2, paramX); // Invoke a method on the server called "ServerMethod" and pass parameters to it. 

var result = await connection.InvokeAsync<MyResult>("ServerMethod", param1, param2, paramX); // Invoke a method on the server called "ServerMethod", pass parameters to it and get the result back.

Contributions and feedback

Please feel free to use the component, open issues, fix bugs or provide feedback.

Contributors

The following people are the maintainers of the Blazor Extensions projects: