SpawnDev.BlazorJS.WebTorrents 2.0.2

dotnet add package SpawnDev.BlazorJS.WebTorrents --version 2.0.2
                    
NuGet\Install-Package SpawnDev.BlazorJS.WebTorrents -Version 2.0.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.WebTorrents" Version="2.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SpawnDev.BlazorJS.WebTorrents" Version="2.0.2" />
                    
Directory.Packages.props
<PackageReference Include="SpawnDev.BlazorJS.WebTorrents" />
                    
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.WebTorrents --version 2.0.2
                    
#r "nuget: SpawnDev.BlazorJS.WebTorrents, 2.0.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.WebTorrents@2.0.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.WebTorrents&version=2.0.2
                    
Install as a Cake Addin
#tool nuget:?package=SpawnDev.BlazorJS.WebTorrents&version=2.0.2
                    
Install as a Cake Tool

SpawnDev.BlazorJS.WebTorrents

NuGet version

SpawnDev.BlazorJS.WebTorrents brings the amazing WebTorrent library to Blazor WebAssembly.

WebTorrent is a streaming torrent client for node.js and the browser. YEP, THAT'S RIGHT. THE BROWSER. It's written completely in JavaScript – the language of the web – so the same code works in both runtimes.

Features

  • 🚀 Streaming torrents in your Blazor WebAssembly applications
  • 📦 Full WebTorrent API access through strongly-typed C# wrappers
  • 🎬 Stream video and audio directly from torrents
  • 📁 Download and seed files entirely in the browser
  • 🔄 Real-time progress tracking with events
  • 💾 IndexedDB storage support for persistent downloads
  • 🎯 Selective file downloading - choose which files to download
  • 🌐 WebRTC peer-to-peer connections
  • 📊 Statistics and metrics - track download/upload speeds, peer counts, etc.
  • 🎨 Intellisense support with comprehensive XML documentation

Demo

Live Demo

Prerequisites

  • .NET 8.0 or later
  • Blazor WebAssembly project
  • Modern web browser with WebRTC support
  • SpawnDev.BlazorJS (automatically installed as a dependency)

Installation

1. Install the NuGet package

dotnet add package SpawnDev.BlazorJS.WebTorrents

Or via Package Manager Console:

Install-Package SpawnDev.BlazorJS.WebTorrents

2. Configure your Program.cs

Update your Program.cs to register the required services:

using SpawnDev.BlazorJS;
using SpawnDev.BlazorJS.WebTorrents;

var builder = WebAssemblyHostBuilder.CreateDefault(args);

// Register BlazorJSRuntime (required)
builder.Services.AddBlazorJSRuntime();

// Register WebTorrent service
builder.Services.AddWebTorrentService();

// Initialize BlazorJSRuntime instead of standard RunAsync()
await builder.Build().BlazorJSRunAsync();

3. Advanced Configuration (Optional)

You can customize WebTorrent behavior with options:

builder.Services.AddWebTorrentService(new WebTorrentOptions
{
    DownloadLimit = 50000,  // Download speed limit in bytes/sec
    Tracker = new TrackerClientOptions
    {
        // Set default trackers for seeding
        Announce = new string[] 
        { 
            "wss://tracker.webtorrent.dev",
            "wss://tracker.openwebtorrent.com"
        },
    }
}, webTorrentService =>
{
    // Enable verbose logging
    webTorrentService.Verbose = true;
});

Quick Start

Basic Usage in a Razor Component

@page "/torrents"
@inject WebTorrentService WebTorrentService
@implements IDisposable

<h3>Torrent Downloader</h3>

@if (torrent != null)
{
    <div>
        <p>Name: @torrent.Name</p>
        <p>Progress: @((torrent.Progress * 100).ToString("F2"))%</p>
        <p>Download Speed: @FormatBytes(torrent.DownloadSpeed)/s</p>
        <p>Upload Speed: @FormatBytes(torrent.UploadSpeed)/s</p>
        <p>Peers: @torrent.NumPeers</p>
    </div>
}

@code {
    private Torrent? torrent;

    protected override async Task OnInitializedAsync()
    {
        // Wait for WebTorrent to be ready
        await WebTorrentService.Ready;

        // Add a torrent
        var magnetUri = "magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c"; // Big Buck Bunny
        torrent = WebTorrentService.Client!.Add(magnetUri);

        // Subscribe to events
        torrent.On("download", () => StateHasChanged());
        torrent.On("upload", () => StateHasChanged());
    }

    private string FormatBytes(double bytes)
    {
        string[] sizes = { "B", "KB", "MB", "GB" };
        int order = 0;
        while (bytes >= 1024 && order < sizes.Length - 1)
        {
            order++;
            bytes /= 1024;
        }
        return $"{bytes:F2} {sizes[order]}";
    }

    public void Dispose()
    {
        torrent?.Dispose();
    }
}

Common Usage Examples

Download a Torrent

await WebTorrentService.Ready;

var torrent = WebTorrentService.Client!.Add("magnet:?xt=urn:btih:...");

torrent.On("ready", () =>
{
    Console.WriteLine($"Torrent ready: {torrent.Name}");
    Console.WriteLine($"Files: {torrent.Files.Length}");
});

torrent.On("done", () =>
{
    Console.WriteLine("Download complete!");
});

Seed a File

await WebTorrentService.Ready;

// Create a torrent from files
using var fileInput = new HTMLInputElement();
fileInput.Type = "file";
fileInput.Multiple = true;
fileInput.Click();

fileInput.OnChange += async () =>
{
    var files = fileInput.Files!;
    var torrent = WebTorrentService.Client!.Seed(files);

    torrent.On("ready", () =>
    {
        Console.WriteLine($"Seeding: {torrent.MagnetURI}");
    });
};

Stream Video from a Torrent

await WebTorrentService.Ready;

var torrent = WebTorrentService.Client!.Add(magnetUri);

torrent.On("ready", () =>
{
    // Find the video file
    var videoFile = torrent.Files.FirstOrDefault(f => 
        f.Name.EndsWith(".mp4") || f.Name.EndsWith(".webm"));

    if (videoFile != null)
    {
        // Create a blob URL for the video
        var blobUrl = videoFile.StreamURL;

        // Use with HTML video element
        // <video src="@blobUrl" controls></video>
    }
});

Download Specific Files Only

var torrent = WebTorrentService.Client!.Add(magnetUri, new TorrentOptions
{
    // Deselect all files initially
    DeselectedFiles = true
});

torrent.On("ready", () =>
{
    // Select only specific files to download
    foreach (var file in torrent.Files)
    {
        if (file.Name.EndsWith(".mp4"))
        {
            file.Select();
        }
    }
});

Track Download Progress

var torrent = WebTorrentService.Client!.Add(magnetUri);

torrent.On("download", () =>
{
    Console.WriteLine($"Progress: {torrent.Progress * 100:F2}%");
    Console.WriteLine($"Downloaded: {torrent.Downloaded} bytes");
    Console.WriteLine($"Speed: {torrent.DownloadSpeed} bytes/sec");
    Console.WriteLine($"Peers: {torrent.NumPeers}");
    Console.WriteLine($"Time remaining: {torrent.TimeRemaining} ms");
});

Remove/Destroy a Torrent

// Remove torrent (keeps files)
WebTorrentService.Client!.Remove(torrent);

// Destroy torrent (deletes files)
await torrent.DestroyAsync();

API Overview

WebTorrentService

The main service for managing torrents.

Key Properties:

  • Client - The WebTorrent client instance
  • Ready - Task that completes when the service is initialized
  • Verbose - Enable verbose logging

Key Events:

  • OnTorrentAdd - Fired when a torrent is added
  • OnTorrentRemove - Fired when a torrent is removed
  • OnTorrentWireAdd - Fired when a peer connection is established
  • OnTorrentWireRemove - Fired when a peer connection is closed

Torrent

Represents a torrent.

Key Properties:

  • Name - Torrent name
  • InfoHash - Info hash
  • MagnetURI - Magnet URI
  • Files - Array of files in the torrent
  • Progress - Download progress (0-1)
  • DownloadSpeed - Current download speed in bytes/sec
  • UploadSpeed - Current upload speed in bytes/sec
  • Downloaded - Total bytes downloaded
  • Uploaded - Total bytes uploaded
  • NumPeers - Number of connected peers
  • Ready - True when metadata is loaded

Key Methods:

  • Select() - Resume downloading
  • Deselect() - Pause downloading
  • DestroyAsync() - Destroy torrent and delete files
  • Pause() - Pause the torrent
  • Resume() - Resume the torrent

Key Events:

  • ready - Fired when torrent metadata is ready
  • done - Fired when all files are downloaded
  • download - Fired on download progress
  • upload - Fired on upload progress
  • wire - Fired when a new peer connects

File

Represents a file within a torrent.

Key Properties:

  • Name - File name
  • Path - File path
  • Length - File size in bytes
  • Downloaded - Bytes downloaded
  • Progress - Download progress (0-1)
  • StreamURL - Blob URL for streaming

Key Methods:

  • Select() - Mark file for download
  • Deselect() - Exclude file from download
  • GetBlobURL() - Get a blob URL for the file
  • GetBlob() - Get file as a Blob

Documentation

SpawnDev.BlazorJS.WebTorrents is a collection of JSObject wrappers that provide strongly-typed access to the JavaScript WebTorrent library. The C# API closely mirrors the JavaScript API with full IntelliSense support.

Additional Resources:

Browser Support

WebTorrent (and thus this library) requires WebRTC support. All modern browsers support WebRTC:

  • ✅ Chrome/Edge (Chromium-based)
  • ✅ Firefox
  • ✅ Safari
  • ✅ Opera

Important Notes

  • Blazor WebAssembly Only: This library only works in Blazor WebAssembly projects. It will not work in Blazor Server.
  • Resource Management: Always dispose of Torrent and File objects when done to free up resources.
  • HTTPS Required: WebRTC requires HTTPS in production environments (localhost works with HTTP during development).
  • Storage: Downloaded files are stored in the browser's IndexedDB by default.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests at the GitHub repository.

  • SpawnDev.BlazorJS - The foundation library for Blazor WebAssembly JavaScript interop
  • WebTorrent - The original JavaScript WebTorrent library

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

Acknowledgments

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 is compatible.  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 is compatible.  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

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
2.0.2 29 2/16/2026
2.0.1 31 2/14/2026
2.0.0 65 2/12/2026
1.12.0 86 2/11/2026
1.11.0 94 1/21/2026
1.10.0 421 11/19/2025
1.9.0 167 10/24/2025
1.8.0 205 9/25/2025
1.7.0 267 4/20/2025
1.6.4 189 1/20/2025
1.6.3 183 12/4/2024
1.6.2 206 11/29/2024
1.6.1 117 11/4/2024
1.6.0 132 10/11/2024
1.5.1 176 8/1/2024
1.5.0 150 7/26/2024
1.4.1 206 7/23/2024
1.4.0 198 7/17/2024
1.3.2 193 7/4/2024
1.3.1 183 6/26/2024
Loading failed