Back
BitTorrent Client
A functional BitTorrent Client built from scratch in node.js and Typescript without using any third-party BitTorrent libraries. Capable of downloading and seeding files across a peer-to-peer network.
Overview
This was inspired by the emails that John Crickett sends out to challenge you to become a better developer. The goal for this was to build a functional BitTorrent client that can connect to peers and download files via .torrent files. I have since expanded this to include magnet link support and a frontend UI.
Features
- Multi-torrent support - Handles multiple torrents at the same time.
- Magnet link support - Handles magnet links to download torrents.
- UDP tracker support - Implements BEP 15 connection and handshake flow, distinct from the HTTP tracker protocol.
- Seeding - Implements the full upload side of the protocol, so the client gives back to the swarm rather than just consuming it.
- Resume interrupted downloads - Persistent downloads, downloads survive restarts without requesting completed pieces.
- Electron desktop frontend - A minimal desktop application for proof of concept.
How It Works
A .torrent file is read in, using a custom bencoding implementation, trackers communicate over HTTP or UDP (BEP15), and peer connections are established via the BitTorrent protocol.
An Orchestration layer coordinates piece requests across peers, managing state across concurrent connections. Once all pieces are verified against their SHA1 hashes, they are assembled into the final file structure and the torrent is marked as complete.
Tech Stack
- Node.js - The BitTorrent logic
- Electron - For Desktop App functionality
- React - For frontend UI
What I Learned
Building this forced me to work closer to the metal than Web Development usually does. Manually parsing Buffers, managing sockets, and learning the hard way that writing to disk too frequently can silently strangle your download speed.
The protocol is also unforgiving, sending the wrong message type to a peer and they'll just disconnect, no explanation. Debugging that kind of thing taught me to read specs carefully.
The biggest lesson was how fast a single coordinator class turns into a mess. By the end it was juggling piece tracking, peer state, and file I/O all at once, pulling it apart was painful but worth it.