SpawnDev.BlazorJS 1.2.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package SpawnDev.BlazorJS --version 1.2.5
                    
NuGet\Install-Package SpawnDev.BlazorJS -Version 1.2.5
                    
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="SpawnDev.BlazorJS" Version="1.2.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SpawnDev.BlazorJS" Version="1.2.5" />
                    
Directory.Packages.props
<PackageReference Include="SpawnDev.BlazorJS" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SpawnDev.BlazorJS --version 1.2.5
                    
#r "nuget: SpawnDev.BlazorJS, 1.2.5"
                    
#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.
#addin nuget:?package=SpawnDev.BlazorJS&version=1.2.5
                    
Install SpawnDev.BlazorJS as a Cake Addin
#tool nuget:?package=SpawnDev.BlazorJS&version=1.2.5
                    
Install SpawnDev.BlazorJS as a Cake Tool

SpawnDev.BlazorJS

NuGet

Supports .Net 7

An easy Javascript interop library desgined specifcally for client side Blazor.

  • Use Javascript libraries in Blazor without writing any Javascript code.

  • Alternative access to IJSRuntime JS is globally available without injection and is usable on the first line of Program.cs

  • Get and set global proeprties via JS.Set and JS.Get

  • Create new Javascript objects with JS.New

  • Get and set object properties via IJSInProcessObjectReference extended methods

  • Create Callbacks that can be sent to Javascript event listeners or assigned to javascript variables

  • Easily call Services in separate threads with WebWorkers and SharedWebWorkers

NOTE: The below code shows quick examples. Some objects implement IDisposable, such as all JSObject, IJSInProcessObjectReference, and Callback, and need to be disposed when no longer used.

Firefox WebWorkers note:
Firefox does not support dynamic modules in workers, which originally made BlazorJS.WebWorkers fail in that browser. I wrote code that changes the scripts on the fly before they are loaded to workaround this limitation until Firefox finishes worker module intergration.

https://bugzilla.mozilla.org/show_bug.cgi?id=1540913#c6
https://bugzilla.mozilla.org/show_bug.cgi?id=1247687

JS

// Get Set
var innerHeight = JS.Get<int>("window.innerHeight");
JS.Set("document.title", "Hello World!");

// Call
var item = JS.Call<string?>("localStorage.getItem", "itemName");
JS.CallVoid("addEventListener", "resize", Callback.Create(() => Console.WriteLine("WindowResized"), _callBacks));

IJSInProcessObjectReference extended

// Get Set
var window = JS.Get<IJSInProcessObjectReference>("window");
window.Set("myVar", 5);
var myVar = window.Get<int>("myVar");

// Call
window.CallVoid("addEventListener", "resize", Callback.Create(() => Console.WriteLine("WindowResized")));

Create a new Javascript object

var worker = JS.New("Worker", myWorkerScript);

Pass callbacks to Javascript

JS.Set("testCallback", Callback.Create<string>((strArg) => {
    Console.WriteLine($"Javascript sent: {strArg}");
    // this prints "Hello callback!"
}));
// in Javascript
testCallback('Hello callback!');

JSObject

Use the extended functions of IJSInProcessObjectReference to work with Javascript objects or use the growing library of over 100 of the most common Javascript objects, including ones for Window, HTMLDocument, WebStorage (locaStorage and sessionStorage), WebGL, WebRTC, and more in SpawnDev.BlazorJS.JSObjects. JSObjects are wrappers around IJSInProcessObjectReference that allow strongly typed use.

Custom JSObjects

Implement your own JSObject classes for Javascript objects not already available in the BlazorJS.JSObjects library.

Instead of this (simple but not as reusable)

var audio = JS.New("Audio", "https://some_audio_online");
audio.CallVoid("play");

Do this...
Create a custom JSObject class

[JsonConverter(typeof(JSObjectConverter<Audio>))]
public class Audio : JSObject
{
    public Audio(IJSInProcessObjectReference _ref) : base(_ref) { }
    public Audio(string url) : base(JS.New("Audio", url)) { }
    public void Play() => JSRef.CallVoid("play");
}

Then use your new object

var audio = new Audio("https://some_audio_online");
audio.Play();

SpawnDev.BlazorJS.WebWorkers

NuGet

Run CPU intensive tasks on a dedicated worker or on a shared worker with WebWorkers!

Example WebWorkerService setup and usage

// Program.cs
...
using SpawnDev.BlazorJS;
using SpawnDev.BlazorJS.WebWorkers;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
if (JS.IsWindow)
{
    // we can skip adding dom objects in non UI threads
    builder.RootComponents.Add<App>("#app");
    builder.RootComponents.Add<HeadOutlet>("head::after");
}
// add services
builder.Services.AddSingleton((sp) => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// SpawnDev.BlazorJS.WebWorkers
builder.Services.AddSingleton<WebWorkerService>();
// app specific services...
builder.Services.AddSingleton<MathsService>();
// build 
WebAssemblyHost host = builder.Build();
// init WebWorkerService
var workerService = host.Services.GetRequiredService<WebWorkerService>();
await workerService.InitAsync();
await host.RunAsync();

WebWorker


// Create a WebWorker
var webWorker = await workerService.GetWebWorker();

// Call a registered service on the worker thread with your arguments
// Action types can be passed for progress reporting
var result = await webWorker.InvokeAsync<MathsService, string>("CalculatePiWithActionProgress", piDecimalPlaces, new Action<int>((i) =>
{
    piProgress = i;
    StateHasChanged();
}));

SharedWebWorker

Calling GetSharedWebWorker in another window with the same sharedWorkerName will return the same SharedWebWorker

// Create or get SHaredWebWorker with the provided sharedWorkerName
var sharedWebWorker = await workerService.GetSharedWebWorker("workername");

// Just like WebWorker but shared
// Call a registered service on the worker thread with your arguments
var result = await sharedWebWorker.InvokeAsync<MathsService, string>("CalculatePiWithActionProgress", piDecimalPlaces, new Action<int>((i) =>
{
    piProgress = i;
    StateHasChanged();
}));

Send events

// Optionally listen for event messages
worker.OnMessage += (sender, msg) =>
{
    if (msg.TargetName == "progress")
    {
        PiProgress msgData = msg.GetData<PiProgress>();
        piProgress = msgData.Progress;
        StateHasChanged();
    }
};

// From SharedWebWorker or WebWorker threads send an event to conencted parents
workerService.SendEventToParents("progress", new PiProgress { Progress = piProgress });

// Or on send an event to a connected worker
webWorker.SendEvent("progress", new PiProgress { Progress = piProgress });

Inspired by Tewr's BlazorWorker implementation. Thank you! I wrote my implementation from scratch as I needed workers in .Net 7.
https://github.com/Tewr/BlazorWorker

BlazorJS and WebWorkers Demo
https://blazorjs.spawndev.com/

Buy me a coffee

paypal

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (12)

Showing the top 5 NuGet packages that depend on SpawnDev.BlazorJS:

Package Downloads
SpawnDev.BlazorJS.WebWorkers

Call Services and static methods in separate threads with WebWorkers and SharedWebWorkers. Run Blazor WASM in the ServiceWorker.

SpawnDev.BlazorJS.WebTorrents

WebTorrents in Blazor WebAssembly

SpawnDev.BlazorJS.FFmpegWasm

SpawnDev.BlazorJS.FFmpegWasm is a Blazor WASM wrapper around ffmpeg.wasm and contains only the base ffmpeg.js and 814.ffmpeg.js files.

SpawnDev.BlazorJS.VisNetwork

VisNetwork in Blazor WebAssembly

SpawnDev.BlazorJS.SimplePeer

SimplePeer WebRTC video, voice, and data channels for Blazor WebAssembly

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.17.0 303 4/29/2025
2.16.0 538 4/26/2025
2.15.0 357 4/21/2025
2.14.0 206 4/20/2025
2.13.3 121 4/19/2025
2.13.2 91 4/19/2025
2.13.1 86 4/19/2025
2.13.0 195 4/18/2025
2.12.2 208 4/15/2025
2.12.1 315 4/12/2025
2.12.0 192 4/8/2025
2.11.1 200 4/4/2025
2.11.0 203 4/2/2025
2.10.0 509 4/2/2025
2.9.0 209 3/29/2025
2.8.0 167 3/20/2025
2.7.0 198 3/2/2025
2.6.0 495 2/17/2025
2.5.39 427 2/2/2025
2.5.38 135 1/31/2025
2.5.37 180 1/22/2025
2.5.36 228 1/15/2025
2.5.35 290 1/6/2025
2.5.34 132 1/4/2025
2.5.33 192 1/1/2025
2.5.32 135 12/30/2024
2.5.31 179 12/18/2024
2.5.30 1,474 12/4/2024
2.5.22 377 11/25/2024
2.5.21 143 11/21/2024
2.5.20 138 11/20/2024
2.5.19 150 11/18/2024
2.5.18 125 11/17/2024
2.5.17 133 11/16/2024
2.5.16 123 11/15/2024
2.5.15 124 11/15/2024
2.5.14 139 11/14/2024
2.5.13 146 11/13/2024
2.5.12 152 11/10/2024
2.5.11 508 10/31/2024
2.5.10 4,497 10/9/2024
2.5.9 254 9/27/2024
2.5.8 6,658 8/13/2024
2.5.7 125 8/13/2024
2.5.6 111 8/8/2024
2.5.5 297 8/7/2024
2.5.4 159 8/6/2024
2.5.3 127 8/5/2024
2.5.2 128 8/5/2024
2.5.1 273 7/26/2024
2.5.0 154 7/26/2024
2.4.7 146 7/24/2024
2.4.6 272 7/22/2024
2.4.5 216 7/19/2024
2.4.4 180 7/18/2024
2.4.3 287 7/16/2024
2.4.2 129 7/15/2024
2.4.0 128 7/15/2024
2.3.8 133 7/14/2024
2.3.7 1,149 7/9/2024
2.3.6 162 7/8/2024
2.3.5 172 7/6/2024
2.3.4 325 7/4/2024
2.3.3 419 6/23/2024
2.3.2 349 6/16/2024
2.3.1 275 6/13/2024
2.3.0 210 6/12/2024
2.2.106 279 6/5/2024
2.2.105 318 5/31/2024
2.2.104 202 5/30/2024
2.2.103 191 5/29/2024
2.2.102 198 5/28/2024
2.2.101 166 5/22/2024
2.2.100 193 5/17/2024
2.2.99 155 5/17/2024
2.2.98 163 5/16/2024
2.2.97 179 5/15/2024
2.2.96 129 5/14/2024
2.2.95 162 5/13/2024
2.2.94 1,564 5/11/2024
2.2.93 162 5/7/2024
2.2.92 158 5/7/2024
2.2.91 180 5/3/2024
2.2.90 120 5/3/2024
2.2.89 106 5/2/2024
2.2.88 127 5/2/2024
2.2.87 348 4/26/2024
2.2.86 166 4/26/2024
2.2.85 530 4/18/2024
2.2.84 150 4/18/2024
2.2.83 187 4/16/2024
2.2.82 513 4/8/2024
2.2.81 163 4/8/2024
2.2.80 176 4/7/2024
2.2.79 166 4/6/2024
2.2.78 167 4/5/2024
2.2.77 178 4/5/2024
2.2.76 176 4/4/2024
2.2.75 155 4/4/2024
2.2.73 155 4/3/2024
2.2.72 159 4/3/2024
2.2.71 184 4/3/2024
2.2.70 154 4/2/2024
2.2.69 350 4/1/2024
2.2.68 187 3/29/2024
2.2.67 322 3/27/2024
2.2.66 211 3/24/2024
2.2.65 178 3/21/2024
2.2.64 236 3/11/2024
2.2.63 203 3/9/2024
2.2.62 166 3/7/2024
2.2.61 177 3/6/2024
2.2.60 164 3/6/2024
2.2.58 217 3/2/2024
2.2.57 715 2/24/2024
2.2.56 201 2/18/2024
2.2.55 160 2/17/2024
2.2.53 165 2/15/2024
2.2.52 176 2/15/2024
2.2.51 163 2/15/2024
2.2.50 146 2/13/2024
2.2.49 1,321 2/2/2024
2.2.48 1,441 12/29/2023
2.2.47 216 12/20/2023
2.2.46 221 12/15/2023
2.2.45 189 12/10/2023
2.2.44 168 12/10/2023
2.2.42 176 12/9/2023
2.2.41 183 12/9/2023
2.2.40 158 12/8/2023
2.2.38 1,211 11/21/2023
2.2.37 599 11/16/2023
2.2.36 147 11/16/2023
2.2.35 187 11/14/2023
2.2.34 131 11/13/2023
2.2.33 104 11/10/2023
2.2.32 123 11/10/2023
2.2.31 100 11/9/2023
2.2.28 116 11/7/2023
2.2.27 192 10/31/2023
2.2.26 228 10/22/2023
2.2.25 120 10/20/2023
2.2.24 113 10/20/2023
2.2.23 116 10/20/2023
2.2.22 110 10/20/2023
2.2.21 118 10/20/2023
2.2.20 111 10/19/2023
2.2.19 108 10/19/2023
2.2.18 104 10/19/2023
2.2.17 212 10/13/2023
2.2.16 504 10/12/2023
2.2.15 110 10/12/2023
2.2.14 157 10/5/2023
2.2.13 132 10/5/2023
2.2.12 107 10/5/2023
2.2.11 296 10/3/2023
2.2.10 234 9/18/2023
2.2.9 105 9/18/2023
2.2.8 297 9/14/2023
2.2.7 130 9/13/2023
2.2.6 10,109 9/6/2023
2.2.5 158 8/30/2023
2.2.4 188 8/26/2023
2.2.3 145 8/20/2023
2.2.2 133 8/18/2023
2.2.1 140 8/11/2023
2.2.0 229 7/17/2023
2.1.15 156 5/26/2023
2.1.14 136 5/20/2023
2.1.13 153 4/26/2023
2.1.12 215 4/21/2023
2.1.11 139 4/19/2023
2.1.10 158 4/19/2023
2.1.8 192 4/10/2023
2.1.7 216 3/27/2023
2.1.6 205 3/24/2023
2.1.5 174 3/23/2023
2.1.4 174 3/23/2023
2.1.3 181 3/23/2023
2.1.2 182 3/21/2023
2.1.0 181 3/21/2023
2.0.3 177 3/21/2023
2.0.2 174 3/20/2023
2.0.1 186 3/20/2023
2.0.0 181 3/20/2023
1.9.2 188 3/14/2023
1.8.1 182 3/11/2023
1.8.0 176 3/10/2023
1.7.1 177 3/10/2023
1.7.0 184 3/8/2023
1.6.4 190 3/1/2023
1.6.3 403 1/31/2023
1.6.2 414 1/24/2023
1.6.1 440 1/11/2023
1.6.0 435 1/11/2023
1.5.0 499 12/23/2022
1.4.0 451 12/20/2022
1.3.0 452 12/16/2022
1.2.7 393 12/16/2022
1.2.5 407 12/14/2022
1.2.4.1 389 12/13/2022
1.2.4 398 12/13/2022
1.2.3 389 12/13/2022
1.2.1 378 12/10/2022
1.2.0 381 12/10/2022
1.1.0 372 12/6/2022
1.0.0 382 12/6/2022