Skip to main content

Methods

torrent.select(start, end, [priority], [notify])

Selects a range of pieces to prioritize.
start
number
required
Start piece index (inclusive)
end
number
required
End piece index (inclusive)
priority
number
Priority level (higher = more important)
notify
function
Callback called when selection is updated
torrent.select(0, 10, 1, () => {
  console.log('Selection updated')
})

torrent.deselect(start, end)

Deprioritizes a range of previously selected pieces.
start
number
required
Start piece index
end
number
required
End piece index
torrent.deselect(0, 10)

torrent.critical(start, end)

Marks a range of pieces as critical priority to be downloaded ASAP.
start
number
required
Start piece index
end
number
required
End piece index
torrent.critical(5, 15)

torrent.addPeer(peer)

Manually add a peer to the torrent swarm.
peer
string | SimplePeer
required
Peer address string (‘ip:port’) or SimplePeer instance for WebRTC
return
boolean
Returns true if peer was added, false if blocked
const added = torrent.addPeer('192.168.1.100:6881')
if (added) {
  console.log('Peer added')
}

torrent.addWebSeed(urlOrConn)

Add a web seed to the torrent swarm.
urlOrConn
string | object
required
Web seed URL or custom connection object
torrent.addWebSeed('https://example.com/files/')

torrent.removePeer(peer)

Remove a peer from the torrent swarm.
peer
string | SimplePeer
required
Peer address, peer ID (hex string), or SimplePeer instance
torrent.removePeer('192.168.1.100:6881')

torrent.pause()

Temporarily stop connecting to new peers.
This does not pause existing connections or their data streams.
torrent.pause()

torrent.resume()

Resume connecting to new peers.
torrent.resume()

torrent.rescanFiles([callback])

Verify the hashes of all pieces in the store and update the bitfield.
callback
function
Called when scan is complete
torrent.rescanFiles(err => {
  if (err) throw err
  console.log('Rescan complete')
})

torrent.destroy([opts], [callback])

Remove the torrent from its client.
opts
object
callback
function
Called when torrent is fully destroyed
torrent.destroy({ destroyStore: true }, err => {
  if (err) throw err
  console.log('Torrent destroyed and files deleted')
})

Properties

torrent.name

name
string
Name of the torrent

torrent.infoHash

infoHash
string
Info hash of the torrent (hex string)

torrent.magnetURI

magnetURI
string
Magnet URI of the torrent

torrent.torrentFile

torrentFile
Uint8Array
.torrent file of the torrent

torrent.torrentFileBlob

torrentFileBlob
Blob
.torrent file as a Blob (useful for creating Blob URLs)

torrent.files

files
Array<File>
Array of all files in the torrent. See File API.
torrent.files.forEach(file => {
  console.log(file.name, file.length)
})

torrent.pieces

pieces
Array<Piece | null>
Array of all pieces. Verified pieces are null.

torrent.announce

announce
Array<string>
Array of all tracker server URLs

torrent.length

length
number
Sum of the file lengths (in bytes)

torrent.pieceLength

pieceLength
number
Length in bytes of every piece except the last one

torrent.lastPieceLength

lastPieceLength
number
Length in bytes of the last piece

torrent.path

path
string
Torrent download location

torrent.ready

ready
boolean
True when torrent is ready to be used (metadata is available and store is ready)

torrent.paused

paused
boolean
True when torrent has stopped connecting to new peers

torrent.done

done
boolean
True when all torrent files have been downloaded

torrent.progress

progress
number
Torrent download progress, from 0 to 1

torrent.downloadSpeed

downloadSpeed
number
Torrent download speed, in bytes/sec

torrent.uploadSpeed

uploadSpeed
number
Torrent upload speed, in bytes/sec

torrent.downloaded

downloaded
number
Total verified bytes received from peers

torrent.uploaded

uploaded
number
Total bytes uploaded to peers

torrent.received

received
number
Total bytes received from peers (including invalid data)

torrent.ratio

ratio
number
Torrent seed ratio (uploaded / downloaded)

torrent.timeRemaining

timeRemaining
number
Time remaining for download to complete (in milliseconds). Returns Infinity if download speed is 0.

torrent.numPeers

numPeers
number
Number of peers in the torrent swarm

torrent.maxWebConns

maxWebConns
number
Max number of simultaneous connections per web seed

torrent.created

created
Date
Date of creation of the torrent

torrent.createdBy

createdBy
string
Author of the torrent

torrent.comment

comment
string
Comment optionally set by the author

Events

See the Events reference for all torrent events.