DEV Community

Todd Tanner
Todd Tanner

Posted on

Pure C# BitTorrent/WebTorrent client and server. No JavaScript dependencies. Desktop + Browser.

SpawnDev.WebTorrent 1.0.0

Pure C# BitTorrent/WebTorrent client and server. No JavaScript dependencies. Desktop + Browser.

Packages

Package NuGet
SpawnDev.WebTorrent Client library — torrents, peers, streaming
SpawnDev.WebTorrent.Server Server library — tracker, web seed

What's In This Release

Pure C# BitTorrent Implementation

  • Complete BitTorrent wire protocol (handshake, bitfield, have, request, piece, cancel, extensions)
  • Bencode encoder/decoder with raw key tracking for info hash computation
  • .torrent file creation and parsing (single-file, multi-file, magnet URIs, Base32)
  • SHA-1 piece verification — every downloaded piece is hash-verified
  • BEP 10 extension protocol framework (ut_metadata, ut_pex)

Random-Access Streaming

  • ModelStream.ReadAsync(offset, length) — read any byte range from a torrent file as it downloads
  • Pieces download on demand when read — only fetch what you need
  • Cross-piece reads handled transparently
  • Designed for ML model weight loading — tested with forward seek, backward seek, large multi-piece spans, and real ML weight access patterns

Client Library (SpawnDev.WebTorrent)

  • WebTorrentClient — full client with peer ID generation, transport management
  • PieceManager — piece selection, block tracking, SHA-1 verification, storage
  • DownloadCoordinator — ties peers + web seeds together with priority requests
  • ModelTorrentClient — high-level API for ML model delivery
  • WebSocketTrackerClient — WebSocket tracker with WebRTC signaling relay
  • WebSeedConnection — HTTP range request fallback (BEP 17/19)
  • TcpTransport — desktop TCP transport
  • WebRtcTransport — browser WebRTC transport (structural, JS interop via SpawnDev.BlazorJS)
  • PeerCoordinator — full signaling flow: tracker → WebRTC → wire protocol → piece exchange
  • MemoryChunkStore + FileChunkStore — pluggable storage backends

Server Library (SpawnDev.WebTorrent.Server)

  • TorrentTracker — WebSocket tracker with WebRTC signaling relay for browser peers
  • WebSeedServer — serves torrent pieces over HTTP range requests
  • MapWebTorrentServer() — one-line ASP.NET endpoint registration

HuggingFace Integration (SpawnDev.WebTorrent.Server.HuggingFace)

  • HuggingFaceProxy — fetches model files from HuggingFace CDN, caches locally
  • Auto-generates .torrent files with piece hashes on demand
  • Magnet URI generation with tracker + web seed URLs
  • .torrent file caching — avoids regeneration on repeated requests
  • Endpoints: /hf/, /torrent/, /magnet/, /hf-stats

Live Production Server

  • hub.spawndev.com — live tracker + HuggingFace proxy serving AI models
  • SSL terminated via HAProxy + LetsEncrypt
  • Self-contained Linux x64 deployment

Test Suite

  • 49 tests passing in Chromium via PlaywrightMultiTest
  • 43 unit tests + 6 integration tests against live server
  • Stream seeking tests: forward, backward, large span, ML weight loading pattern
  • Full end-to-end: browser → server → HuggingFace → cache → .torrent → download → verify

Demo App

  • Live Demo — dark theme, live server stats, unit tests
  • Blazor WebAssembly — runs entirely in the browser
  • GitHub Pages deployment

Why This Exists

AI models are big — 100MB to 2GB+. CDNs can't scale when every user downloads the same model. SpawnDev.WebTorrent turns every browser into a peer. The more users, the faster delivery. Built for SpawnDev.ILGPU.ML — GPU-accelerated ML inference for .NET.

What's Next

  • Distributed GPU compute across devices via P2P network
  • AcceleratorType.P2P — 7th SpawnDev.ILGPU backend
  • AI agent communication protocol over wire extensions
  • SpawnDev.WebFS integration (virtual filesystem backed by torrent swarm)
  • DHT peer discovery (Kademlia)
  • BEP 52 (BitTorrent v2 with SHA-256)

Credits

Built by Todd Tanner (@LostBeard) and the SpawnDev team: Claude (Data), Claude (Riker), Claude (Tuvok), and Gemini.

🖖🚀

Top comments (0)