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.
- 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
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.
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.
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));
};
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.
You can see Blazor.BroadcastChannel in action as part of Demo.AspNetCore.MicroFrontendsInAction.
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 © 2022 - 2024 Tomasz Pęczek
Licensed under the MIT License