Blazor.BroadcastChannel
1.0.1
dotnet add package Blazor.BroadcastChannel --version 1.0.1
NuGet\Install-Package Blazor.BroadcastChannel -Version 1.0.1
<PackageReference Include="Blazor.BroadcastChannel" Version="1.0.1" />
paket add Blazor.BroadcastChannel --version 1.0.1
#r "nuget: Blazor.BroadcastChannel, 1.0.1"
// Install Blazor.BroadcastChannel as a Cake Addin #addin nuget:?package=Blazor.BroadcastChannel&version=1.0.1 // Install Blazor.BroadcastChannel as a Cake Tool #tool nuget:?package=Blazor.BroadcastChannel&version=1.0.1
Blazor.BroadcastChannel
This is a HTML5 Broadcast Channel API implementation for Blazor.
The Broadcast Channel API allows sending messages to other browsing contexts on the same origin. It can be thought of as a simple message bus that allows pub/sub semantics between windows/tabs, iframes, web workers, and service workers.
How to use Blazor.BroadcastChannel
Getting Started
- Install the NuGet package
Blazor.BroadcastChannel
.dotnet add package Blazor.BroadcastChannel
- In
Program.cs
addbuilder.Services.AddBroadcastChannel
.... var builder = WebAssemblyHostBuilder.CreateDefault(args); ... builder.Services.AddBroadcastChannel(); ... await builder.Build().RunAsync();
- Add the
Blazor.BroadcastChannel
namespace in_Imports.razor
or the component in which you want to use the Broadcast Channel API.@using Blazor.BroadcastChannel
- Inject the
IBroadcastChannelService
in the component in which you want to use the Broadcast Channel API.@inject IBroadcastChannelService BroadcastChannelService
Creating or Joining a Channel
The IBroadcastChannelService.CreateOrJoinAsync
method allows for joining a broadcast channel. It takes a single parameter, which is the name of the channel. It returns an instance of IBroadcastChannel
which represents the channel.
IBroadcastChannel broadcastChannel = await BroadcastChannelService.CreateOrJoinAsync("checkout:item-added");
If it is the first connection to that broadcast channel, the underlying channel is created.
Sending a Message
To send a message, it is enough to call the IBroadcastChannel.PostMessageAsync
method, which takes any object as an argument.
await broadcastChannel.PostMessageAsync(new CheckoutItem { Sku = Sku, Edition = Edition });
The API doesn't associate any semantics to messages, so it is up to the receiving code to know what kind of messages to expect and how to handle them.
Receiving a Message
In order to receive messages, it is enough to subscribe to IBroadcastChannel.Message
event. The sent object is available as JsonDocument
through BroadcastChannelMessageEventArgs.Data
property.
broadcastChannel.Message += (object? sender, BroadcastChannelMessageEventArgs e) =>
{
Console.WriteLine(JsonSerializer.Serialize(e.Data));
};
Disconnecting a Channel
To leave a broadcast channel, either dispose the IBroadcastChannel
or call IBroadcastChannel.CloseAsync
method.
await broadcastChannel.DisposeAsync();
It is important to disconnect from the channel to allow the underlying JavaScript object to be garbage collected.
Demos
You can see Blazor.BroadcastChannel in action as part of Demo.AspNetCore.MicroFrontendsInAction.
Donating
My blog and open source projects are result of my passion for software development, but they require a fair amount of my personal time. If you got value from any of the content I create, then I would appreciate your support by sponsoring me (either monthly or one-time).
Copyright and License
Copyright © 2022 - 2024 Tomasz Pęczek
Licensed under the MIT License
Product | Versions 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. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.