Blazor.BroadcastChannel 1.0.1

dotnet add package Blazor.BroadcastChannel --version 1.0.1                
NuGet\Install-Package Blazor.BroadcastChannel -Version 1.0.1                
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="Blazor.BroadcastChannel" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.BroadcastChannel --version 1.0.1                
#r "nuget: Blazor.BroadcastChannel, 1.0.1"                
#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 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

NuGet Version

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

  1. Install the NuGet package Blazor.BroadcastChannel.
    dotnet add package Blazor.BroadcastChannel 
    
  2. In Program.cs add builder.Services.AddBroadcastChannel.
    ...
    
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    
    ...
    
    builder.Services.AddBroadcastChannel();
    
    ...
    
    await builder.Build().RunAsync();
    
  3. Add the Blazor.BroadcastChannel namespace in _Imports.razor or the component in which you want to use the Broadcast Channel API.
    @using Blazor.BroadcastChannel
    
  4. 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 © 2022 - 2024 Tomasz Pęczek

Licensed under the MIT License

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.1 14,504 2/22/2024
1.0.0 9,128 8/31/2022