Skip to main content
This guide will walk you through sharing a file between two peers in just a few minutes.

Prerequisites

Before you begin, make sure you have:
  • Node.js v22 or higher installed
  • The p2p-file-share repository cloned
  • A test file to share (or create one)

Quick Setup

1

Create a test file

First, create a test file to share between peers:
echo "Hello from P2P File Share!" > test.txt
Or use any existing file you want to share.
2

Start the seed node

In your first terminal, start a seed node that has the complete file:
npm start -- --port 6881 --file test.txt
You should see output like:
Nodo P2P iniciado. ID: ab12cd34ef56, escuchando en puerto 6881.
Archivo disponible para compartir: "test.txt" (27 bytes). Esperando conexiones de pares...
The seed node calculates the SHA-1 hash of the file automatically. This hash will be used to verify file integrity after download.
3

Start the leecher node

In a second terminal, start a leecher node that will download the file:
npm start -- --port 6882 --file test-downloaded.txt --peer 127.0.0.1:6881
You should see the download progress:
Conectando con peer inicial 127.0.0.1:6881...
Meta de archivo recibida: "test.txt" (27 bytes, 1 piezas). Iniciando descarga...
Pieza 0 recibida (27 bytes). Piezas restantes: 0.
¡Descarga completada! Archivo "test.txt" descargado completamente.
Verificación de integridad: OK (hash coincide).
El nodo continuará corriendo como seed para compartir el archivo con otros peers.
Once the download completes, the leecher automatically becomes a seed and can share the file with other peers.
4

Verify the downloaded file

Check that the file was downloaded correctly:
cat test-downloaded.txt
The content should match your original file exactly.

What Just Happened?

Here’s what occurred behind the scenes:
  1. Handshake: The leecher connected to the seed and exchanged metadata (file name, size, hash, piece size)
  2. Bitfield: The seed announced it has all pieces of the file
  3. Request: The leecher requested piece 0
  4. Piece: The seed sent the piece data (encoded in base64)
  5. Verification: The leecher verified the complete file’s SHA-1 hash matches the seed’s hash

Next Steps

Now that you have a basic P2P network running, try these next:

Add More Peers

Set up a 3+ peer network to see true P2P file distribution

Command Reference

Learn about all available command-line options

Protocol Details

Understand the messaging protocol in depth

Architecture

Learn how the P2P system is designed

Larger Files

For larger files, you’ll see more detailed progress tracking:
npm start -- --port 6881 --file large-video.mp4
Seed output:
Nodo P2P iniciado. ID: ef78cd90ab12, escuchando en puerto 6881.
Archivo disponible para compartir: "large-video.mp4" (157286400 bytes). Esperando conexiones de pares...
Leecher output:
npm start -- --port 6882 --file downloaded-video.mp4 --peer 127.0.0.1:6881
Conectando con peer inicial 127.0.0.1:6881...
Meta de archivo recibida: "large-video.mp4" (157286400 bytes, 2400 piezas). Iniciando descarga...
Progreso: 10.00% (15728640/157286400 bytes). Velocidad: 1024.5 KB/s
Pieza 0 recibida (65536 bytes). Piezas restantes: 2399.
Pieza 1 recibida (65536 bytes). Piezas restantes: 2398.
...
Progreso: 100.00% (157286400/157286400 bytes). Velocidad: 982.3 KB/s
¡Descarga completada! Archivo "large-video.mp4" descargado completamente.
Verificación de integridad: OK (hash coincide).
Files are automatically split into 64 KiB (65536 bytes) pieces. For files smaller than 64 KiB, the piece size adjusts to match the file size.

Troubleshooting

If you encounter issues, check out the Troubleshooting guide for common problems and solutions.

Build docs developers (and LLMs) love