Simple, robust, streaming bittorrent client.
This module is deprecated. Please use the
webtorrent module instead, which has an equivalent API.
Simple, robust, torrent client that exposes files as streams so you can access file content before a torrent has finished downloading. This module is used by WebTorrent and heavily inspired by the excellent design of torrent-stream by @mafintosh.
- insanely fast
- exposes individual files as streams to access file content before torrent is finished
- sequentially requests pieces from peers (when necessary)
- otherwise, uses rarest-first piece strategy
- download multiple torrents simultaneously, efficiently
- supports advanced torrent client features
- comprehensive test suite (completely offline, so it's reliable and fast)
npm install bittorrent-client
Access files inside a torrent as node.js readable streams.
The client automatically connects to the DHT to fetch torrent metadata (if necessary) and to discover new peers. If the magnet uri or .torrent file contains tracker urls, the client automatically connects to trackers to discover new peers.
var BitTorrentClient = require'bittorrent-client'var client =// "Pride and Prejudice" by Jane Austenclientadd'magnet:?xt=urn:btih:1e69917fbaa2c767bca463a96b5572785c6d8a12'clienton'torrent'// torrent metadata has been fetchedconsole.logtorrentnametorrentfilesforEachconsole.logfilename// get a readable stream of the file contentvar stream = filecreateReadStream
You can pass
end options to
createReadStream to stream only a slice of
// get a stream containing bytess 100-1000 inclusivevar stream = filecreateReadStreamstart: 100end: 1000
By default, no files are downloaded until you call
file.createReadStream. If you want to
download a particular file without creating a stream, call
To download multiple torrents simulataneous, just reuse the same instance of
This will improve the download speed, conserve system resources, and allow internal state like the DHT routing table to be re-used.
// Sintel movie in 4Kvar magnet = 'magnet:?xt=urn:btih:489a21c45f7eb13ad75b3b9bfa0132b1be035f62'clientaddmagnetif !err// torrent metadata has been fetchedconsole.logtorrentname
You can also download from a local torrent file, or a URL to a torrent.
var fs = require'fs'var file = fsreadFileSync'/path/to/file.torrent'clientaddfilevar url = ''clientaddurl
Create a new
opts is specified, then the default options (shown below) will be overridden.
maxPeers: 100 // Max number of peers to connect to (per torrent)peerId: '' // Wire protocol peer ID (otherwise, randomly generated)nodeId: '' // DHT protocol node ID (otherwise, randomly generated)tracker: true // Whether or not to enable trackersdht: true // Whether or not to enable DHTverify: true // Verify previously stored data before starting
Start downloading a torrent. Aliased as
torrentId can be any of the following:
- info hash (as a hex string or Buffer)
- magnet uri (as a utf8 string)
- .torrent file (as a Buffer)
- parsed torrent (from parse-torrent)
- http/https url to a .torrent file (string)
- filesystem path to a .torrent file (string)
ontorrent is specified, then it will be called when this torrent is ready to be
used (i.e. metadata is available). Note: this is distinct from the 'torrent' event which
will fire for all torrents.
If you want access to the torrent object immediately in order to listen to events as the
metadata is fetched from the network, then use the return value of
client.add. If you
just want the file data, then use
ontorrent or the 'torrent' event.
Start seeding a new torrent.
input can be any of the following:
- path to the file or folder on filesystem (string)
- W3C File object (from an
<input>or drag and drop)
- W3C FileList object (basically an array of
- Array of
opts is specified, it should contain the following types of options:
- options for create-torrent (to allow configuration of the .torrent file that is created)
- options for
onseed is specified, it will be called when the client has begun seeding the file.
Emitted when a torrent is ready to be used (i.e. metadata is available and storage is
ready). See the torrent section for more info on what methods a
Remove a torrent from the client. Destroy all connections to peers and delete all saved
file data. Optional
callback is called when file data has been removed.
Destroy the client, including all torrents and connections to peers.
Listen for incoming peers on the specified port. Port defaults to
An array of all torrents in the client.
Return the torrent with the given
torrentId. Easier than searching through the
client.torrents array by hand for the torrent you want.
Aggregate seed ratio for all torrents in the client.
An array of all files in the torrent. See the file section for more info on what methods the file has.
The attached bittorrent-swarm instance.
Adds a peer to the underlying bittorrent-swarm instance.
Selects a range of pieces to prioritize starting with
start and ending with
end (both inclusive)
at the given
notify is an optional callback to be called when the selection is updated
with new data.
Deprioritizes a range of previously selected pieces.
Marks a range of pieces as critical priority to be downloaded ASAP. From
File name, as specified by the torrent. Example: 'some-filename.txt'
File path, as specified by the torrent. Example: 'some-folder/some-filename.txt'
File length (in bytes), as specified by the torrent. Example: 12345
Selects the file to be downloaded, but at a lower priority than files with streams. Useful if you know you need the file at a later stage.
Deselects the file, which means it won't be downloaded unless someone creates a stream for it.
Create a readable stream to the file. Pieces needed by the stream will be prioritized highly and fetched from the swarm first.
You can pass
opts to stream only a slice of a file.
start: startByteend: endByte
end are inclusive.
MIT. Copyright (c) Feross Aboukhadijeh.