SpawnDev.BlazorJS.WebWorkers 1.6.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package SpawnDev.BlazorJS.WebWorkers --version 1.6.2
                    
NuGet\Install-Package SpawnDev.BlazorJS.WebWorkers -Version 1.6.2
                    
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.WebWorkers" Version="1.6.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SpawnDev.BlazorJS.WebWorkers" Version="1.6.2" />
                    
Directory.Packages.props
<PackageReference Include="SpawnDev.BlazorJS.WebWorkers" />
                    
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.WebWorkers --version 1.6.2
                    
#r "nuget: SpawnDev.BlazorJS.WebWorkers, 1.6.2"
                    
#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.
#:package SpawnDev.BlazorJS.WebWorkers@1.6.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SpawnDev.BlazorJS.WebWorkers&version=1.6.2
                    
Install as a Cake Addin
#tool nuget:?package=SpawnDev.BlazorJS.WebWorkers&version=1.6.2
                    
Install as a Cake Tool

NuGet

Package Description Link
SpawnDev.BlazorJS Enhanced Blazor WebAssembly Javascript interop NuGet version
SpawnDev.BlazorJS.WebWorkers Blazor WebAssembly WebWorkers and SharedWebWorkers NuGet version

SpawnDev.BlazorJS

NuGet

An easy Javascript interop library designed specifically for client side Blazor.

Supports Blazor WebAssembly .Net 6 and .Net 7.

  • 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 properties 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

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

JSObjects are wrappers around IJSInProcessReference objects that can be passed to and from Javascript and allow strongly typed access to the underlying object.

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 (localStorage 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");

You can do this...
Create a custom JSObject wrapper

[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

  • Easily call Blazor Services in separate threads with WebWorkers and SharedWebWorkers

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 integration. It is possible some other browsers may have this issue. Issues can be reported here on GitHub.

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

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 connected parents
workerService.SendEventToParents("progress", new PiProgress { Progress = piProgress });

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

Worker Transferable JSObjects

When working with workers in Javascript you can optionally tell Javascript (via the MessagePort.postMessage method) to transfer some of the objects instead of copying them.

WebWorkerService, when calling services on a worker, will transfer any transferable types by default. To disable the transferring of a return value, parameter, or property use the WorkerTransferAttribute.

Example

        public class ProcessFrameResult : IDisposable
        {
            [WorkerTransfer(false)]
            public ArrayBuffer? ArrayBuffer { get; set; }
            public byte[]? HomographyBytes { get; set; }
            public void Dispose(){
                ArrayBuffer?.Dispose();
            }
        }

        [return: WorkerTransfer(false)]
        public async Task<ProcessFrameResult?> ProcessFrame([WorkerTransfer(false)] ArrayBuffer? frameBuffer, int width, int height, int _canny0, int _canny1, double _needlePatternSize)
        {
            var ret = new ProcessFrameResult();
            // ...
            return ret;
        }

In the above example; the WorkerTransferAttribute on the return type set to false will prevent all properties of the return type from being transferred.

Transferable JSObject types

ArrayBuffer
MessagePort
ReadableStream
WritableStream
TransformStream
AudioData
ImageBitmap
VideoFrame
OffscreenCanvas
RTCDataChannel

NOTE: The above code shows quick examples. Some objects implement IDisposable, such as all JSObject, IJSInProcessObjectReference, and Callback, and need to be disposed when no longer used. Disposable objects returned from a Blazor service in a WebWorker or SharedWorker are automatically disposed after the data has been sent to the calling thread.

Support

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 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 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 (4)

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

Package Downloads
SpawnDev.BlazorJS.PeerJS

PeerJS simplifies peer-to-peer data, video, and audio calls in Blazor WebAssembly

SpawnDev.BlazorJS.Photino

Blazor WebAssembly in Photino. Use this package in the Blazor WebAssembly project.

SpawnDev.WebFS

Lets Blazor WebAssembly apps connect to the WebFS tray app running on a user's PC and host a file system via a domain labeled folder.

SpawnDev.BlazorJS.BrowserExtension

Create Manifest V3 web browser extensions using Blazor WebAssembly

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.38.0 0 1/26/2026
2.37.0 84 1/21/2026
2.36.0 73 1/20/2026
2.35.0 128 1/12/2026
2.34.0 91 1/12/2026
2.33.0 84 1/12/2026
2.32.0 98 1/9/2026
2.31.0 104 1/8/2026
2.30.0 88 1/7/2026
2.29.0 93 1/6/2026
2.28.0 92 1/6/2026
2.27.0 113 12/31/2025
2.26.0 442 12/5/2025
2.25.0 606 11/17/2025
2.23.0 335 11/12/2025
2.22.0 279 11/2/2025
2.21.0 3,062 10/2/2025
2.19.0 924 9/1/2025
2.18.0 760 8/22/2025
2.17.1 316 7/11/2025
2.17.0 311 7/6/2025
2.16.0 216 7/2/2025
2.15.0 219 7/2/2025
2.14.0 207 7/1/2025
2.13.0 2,657 4/26/2025
2.12.0 410 4/21/2025
2.11.0 269 4/21/2025
2.10.2 435 4/12/2025
2.10.1 216 4/12/2025
2.10.0 218 4/12/2025
2.9.1 701 4/2/2025
2.9.0 273 4/2/2025
2.8.0 245 4/2/2025
2.7.0 323 3/31/2025
2.6.0 678 2/19/2025
2.5.39 401 2/2/2025
2.5.38 231 1/31/2025
2.5.37 265 1/22/2025
2.5.36 253 1/15/2025
2.5.35 282 1/6/2025
2.5.34 241 1/4/2025
2.5.33 247 1/1/2025
2.5.32 197 12/30/2024
2.5.31 256 12/18/2024
2.5.30 430 12/4/2024
2.5.22 418 11/25/2024
2.5.21 224 11/21/2024
2.5.20 214 11/20/2024
2.5.19 234 11/18/2024
2.5.18 222 11/17/2024
2.5.17 201 11/16/2024
2.5.16 219 11/15/2024
2.5.15 221 11/15/2024
2.5.14 222 11/14/2024
2.5.13 234 11/13/2024
2.5.12 188 11/10/2024
2.5.11 528 10/31/2024
2.5.10 403 10/9/2024
2.5.9 210 9/27/2024
2.5.8 2,607 8/13/2024
2.5.6 161 8/8/2024
2.5.5 204 8/7/2024
2.5.4 213 8/6/2024
2.5.3 216 8/5/2024
2.5.2 191 8/5/2024
2.5.1 254 7/26/2024
2.5.0 207 7/26/2024
2.4.7 207 7/24/2024
2.4.6 210 7/22/2024
2.4.5 268 7/19/2024
2.4.4 225 7/18/2024
2.4.3 249 7/16/2024
2.4.2 212 7/15/2024
2.4.0 208 7/15/2024
2.3.8 223 7/14/2024
2.3.7 271 7/9/2024
2.3.6 223 7/8/2024
2.3.5 241 7/6/2024
2.3.4 222 7/4/2024
2.3.3 243 6/23/2024
2.3.2 232 6/16/2024
2.3.1 356 6/13/2024
2.3.0 208 6/12/2024
2.2.106 252 6/5/2024
2.2.105 243 5/31/2024
2.2.104 237 5/30/2024
2.2.103 215 5/29/2024
2.2.102 244 5/28/2024
2.2.101 252 5/22/2024
2.2.100 278 5/17/2024
2.2.99 232 5/17/2024
2.2.98 251 5/16/2024
2.2.97 248 5/15/2024
2.2.96 216 5/14/2024
2.2.95 224 5/13/2024
2.2.94 218 5/11/2024
2.2.93 254 5/7/2024
2.2.92 235 5/7/2024
2.2.91 256 5/3/2024
2.2.90 192 5/3/2024
2.2.89 166 5/2/2024
2.2.88 193 5/2/2024
2.2.87 259 4/26/2024
2.2.86 254 4/26/2024
2.2.85 275 4/18/2024
2.2.84 241 4/18/2024
2.2.83 245 4/16/2024
2.2.82 242 4/8/2024
2.2.81 261 4/8/2024
2.2.80 261 4/7/2024
2.2.79 265 4/6/2024
2.2.78 258 4/5/2024
2.2.77 260 4/5/2024
2.2.76 234 4/4/2024
2.2.75 232 4/4/2024
2.2.73 206 4/3/2024
2.2.72 241 4/3/2024
2.2.71 246 4/3/2024
2.2.70 231 4/2/2024
2.2.69 373 4/1/2024
2.2.68 209 3/29/2024
2.2.67 284 3/27/2024
2.2.66 280 3/24/2024
2.2.65 243 3/21/2024
2.2.64 279 3/11/2024
2.2.63 263 3/9/2024
2.2.62 257 3/7/2024
2.2.61 253 3/6/2024
2.2.60 252 3/6/2024
2.2.58 295 3/2/2024
2.2.57 328 2/24/2024
2.2.56 285 2/18/2024
2.2.55 249 2/17/2024
2.2.53 258 2/15/2024
2.2.52 251 2/15/2024
2.2.51 241 2/15/2024
2.2.49 1,678 2/2/2024
2.2.48 1,650 12/29/2023
2.2.47 286 12/20/2023
2.2.46 245 12/15/2023
2.2.45 254 12/10/2023
2.2.44 258 12/10/2023
2.2.42 261 12/9/2023
2.2.41 245 12/9/2023
2.2.40 258 12/8/2023
2.2.38 1,219 11/21/2023
2.2.37 821 11/16/2023
2.2.36 185 11/16/2023
2.2.35 245 11/14/2023
2.2.34 190 11/13/2023
2.2.33 155 11/10/2023
2.2.32 163 11/10/2023
2.2.31 147 11/9/2023
2.2.28 177 11/7/2023
2.2.27 225 10/31/2023
2.2.26 254 10/22/2023
2.2.25 168 10/20/2023
2.2.24 173 10/20/2023
2.2.23 182 10/20/2023
2.2.22 191 10/20/2023
2.2.21 173 10/20/2023
2.2.20 163 10/19/2023
2.2.19 167 10/19/2023
2.2.18 152 10/19/2023
2.2.17 265 10/13/2023
2.2.16 576 10/12/2023
2.2.15 172 10/12/2023
2.2.14 176 10/5/2023
2.2.13 175 10/5/2023
2.2.12 150 10/5/2023
2.2.11 374 10/3/2023
2.2.10 248 9/18/2023
2.2.9 187 9/18/2023
2.2.8 352 9/14/2023
2.2.7 180 9/13/2023
2.2.6 12,083 9/6/2023
2.2.5 245 8/30/2023
2.2.4 242 8/26/2023
2.2.3 221 8/20/2023
2.2.2 205 8/18/2023
2.2.1 224 8/11/2023
2.2.0 316 7/17/2023
2.1.15 253 5/26/2023
2.1.14 230 5/20/2023
2.1.13 248 4/26/2023
2.1.12 297 4/21/2023
2.1.11 238 4/19/2023
2.1.10 273 4/19/2023
2.1.8 277 4/10/2023
2.1.7 302 3/27/2023
2.1.6 275 3/24/2023
2.1.5 248 3/23/2023
2.1.4 267 3/23/2023
2.1.3 275 3/23/2023
2.1.2 259 3/21/2023
2.1.0 276 3/21/2023
2.0.3 281 3/21/2023
2.0.2 274 3/20/2023
2.0.1 268 3/20/2023
2.0.0 288 3/20/2023
1.9.2 282 3/14/2023
1.8.1 282 3/11/2023
1.8.0 264 3/10/2023
1.7.1 281 3/10/2023
1.7.0 247 3/8/2023
1.6.4 276 3/1/2023
1.6.3 443 1/31/2023
1.6.2 452 1/24/2023
1.6.1 465 1/11/2023
1.6.0 491 1/11/2023
1.5.0 542 12/23/2022
1.4.0 478 12/20/2022
1.3.0 494 12/16/2022
1.2.7 482 12/16/2022
1.2.5 467 12/14/2022
1.2.4.1 480 12/13/2022
1.2.4 477 12/13/2022
1.2.3 482 12/13/2022