WebTorrent is a streaming torrent client that works in node.js and the browser. YEP, THAT'S RIGHT. The browser. It's written completely in JavaScript – the language of the web – so the same code works in both runtimes.
In browsers, WebTorrent uses WebRTC (data channels) for peer-to-peer transport. It can be used without browser plugins, extensions, or installations. It's Just JavaScript™.
Simply include the webtorrent.min.js
script on your page to start fetching files over
WebRTC using the BitTorrent protocol. See code examples below.
To make BitTorrent work over WebRTC (which is the only p2p transport that works on the web) we made some protocol changes. Currently, WebTorrent clients can only connect to other WebTorrent clients.
We hope established torrent clients (uTorrent, Transmission, Vuze, etc.) will add support for WebRTC and the WebTorrent extensions so they can swarm with peers from both the normal and web networks.
Warning: This is pre-alpha software. Watch/star to follow along with progress.
- Torrent client for node.js & the browser (same npm module!)
- Insanely fast
- Streaming video to many devices (including AirPlay, Chromecast, and VLC player)
- Download multiple torrents simultaneously, efficiently
- Pure Javascript (no native dependencies)
- Exposes files as streams (and fetches pieces on-demand before torrent is finished
- Fetches data from the network on-demand, so streaming and seeking are supported
- Seamlessly switches between sequential and rarest-first piece selection strategy
- Supports advanced torrent client features
- magnet uri support via ut_metadata
- peer discovery via dht, tracker, and ut_pex
- protocol extension api for adding new extensions
- Comprehensive test suite (completely offline, so it's reliable and fast)
- Uses WebRTC data channels for lightweight peer-to-peer communication (no plugins)
- No silos. WebTorrent is a P2P network for the entire web. WebTorrent clients running on one domain can connect to clients on any other domain.
- Stream video torrents into a
<video>
tag (webm (vp8, vp9)
ormp4 (h.264)
)
- Join us in IRC on freenode at
#webtorrent
if you want to help with development, or you just want to hang out with some cool mad science hackers :) - Create a new issue to report bugs
- Fix an issue. Note: WebTorrent is an OPEN Open Source Project!
- Donate bitcoin if you believe in the vision and wish to support the project. 1B6aystcqu8fd6ejzpmMFMPRqH9b86iiwh or Coinbase.
With npm, run:
npm install webtorrent -g
WebTorrent is the first BitTorrent client that works in the browser, using open web standards (no plugins, just HTML5 and WebRTC)!
It's easy to get started!
Downloading a file is simple:
var WebTorrent = require('webtorrent')
var concat = require('concat-stream')
var client = new WebTorrent()
client.download(magnet_uri, function (torrent) {
// Got torrent metadata!
console.log('Torrent info hash:', torrent.infoHash)
torrent.files.forEach(function (file) {
// Get the file data as a Buffer (Uint8Array typed array)
file.createReadStream().pipe(concat(function (buf) {
// Append a link to download the file
var a = document.createElement('a')
a.download = file.name
a.href = URL.createObjectURL(new Blob([ buf ]))
a.textContent = 'download ' + file.name
document.body.appendChild(a)
}))
})
}
Seeding a file is simple, too:
var dragDrop = require('drag-drop/buffer')
var WebTorrent = require('webtorrent')
var client = new WebTorrent()
// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', function (files) {
client.seed(files, function onTorrent (torrent) {
// Client is seeding the file!
console.log('Torrent info hash:', torrent.infoHash)
})
})
WebTorrent works great with browserify, an npm module that let's you use node-style require() to organize your browser code and load modules installed by npm (as seen in the previous examples).
WebTorrent is also available as a standalone script
(webtorrent.min.js
) which exposes WebTorrent
on the window
object, so it can be used with just a script tag:
<script src="webtorrent.min.js"></script>
WebTorrent also works in node.js, using the same npm module! It's mad science!
WebTorrent is available as a command line app Here's how to use it:
$ npm install webtorrent -g
$ webtorrent --help
To download a torrent:
$ webtorrent magnet_uri
$ webtorrent /path/to/file.torrent
To stream a torrent to a device like AirPlay or Chromecast, just pass a flag:
$ webtorrent magnet_uri --airplay
There are many supported streaming options:
--airplay stream to Apple TV (AirPlay)
--chromecast stream to Chromecast
--mplayer stream to MPlayer
--mpv stream to MPV
--omx [jack] stream to omx (jack=local|hdmi)
--vlc stream to VLC
--xbmc stream to XBMC
- Instant – Secure, anonymous, streaming file transfer [code]
- Your app here! (send a PR or open an issue with the URL to your app)
This API should work exactly the same in node and the browser. Open an issue if this is not the case.
Create a new WebTorrent
instance.
If opts
is specified, then the default options (shown below) will be overridden.
{
dht: true, // Whether or not to enable DHT
maxPeers: 100, // Max number of peers to connect to (per torrent)
nodeId: '', // DHT protocol node ID (otherwise, randomly generated)
peerId: '', // Wire protocol peer ID (otherwise, randomly generated)
storage: function // custom storage engine, or false for in-memory engine
tracker: true, // Whether or not to enable trackers
verify: true, // Verify previously stored data before starting
}
Start downloading a new torrent. Aliased as client.download
.
torrentId
can be one of:
- magnet uri (utf8 string)
- torrent file (buffer)
- info hash (hex string or buffer)
- parsed torrent (from parse-torrent)
- http/https url to a torrent file (string)
- filesystem path to a torrent file (string)
If 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
File
objects) - Array of
File
objects
If 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
client.add
(see above)
If 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 torrent
has.
Remove a torrent from the client. Destroy all connections to peers and delete all saved
file data. If callback
is specified, it will be called when file data is removed.
Destroy the client, including all torrents and connections to peers.
Listen for incoming peers on the specified port. Port defaults to 6881
An array of all torrents in the client.
Returns the torrent with the given torrentId
. Convenience method. Easier than
searching through the client.torrents
array.
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.
Alias for client.remove(torrent)
.
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 priority
. 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 start
to end
(both inclusive).
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: startByte,
end: endByte
}
Both start
and end
are inclusive.
Most of the active development is happening inside of small npm modules which are used by WebTorrent.
"When applications are done well, they are just the really application-specific, brackish residue that can't be so easily abstracted away. All the nice, reusable components sublimate away onto github and npm where everybody can collaborate to advance the commons." — substack from "how I write modules"
These are the modules I am writing to make WebTorrent work:
module | tests | version | description |
---|---|---|---|
webtorrent | torrent client (this module) | ||
addr-to-ip-port | cache for addr->ip:port | ||
bittorrent-dht | distributed hash table client | ||
bittorrent-peerid | identify client name/version | ||
bittorrent-protocol | bittorrent protocol stream | ||
bittorrent-swarm | bittorrent connection manager | ||
bittorrent-tracker | bittorrent tracker server/client | ||
buffer | node buffer api for the browser | ||
create-torrent | create .torrent files | ||
ip-set | efficient mutable ip set | ||
load-ip-set | load ip sets from local/network | ||
magnet-uri | parse magnet uris | ||
parse-torrent | parse torrent identifiers | ||
parse-torrent-file | parse .torrent files | ||
simple-peer | simpler webrtc api | ||
simple-websocket | simpler websocket api | ||
string2compact | convert 'hostname:port' to compact | ||
torrent-discovery | find peers via dht and tracker | ||
typedarray-to-buffer | efficient buffer creation | ||
ut_metadata | metadata for magnet uris (ext) | ||
ut_pex | peer discovery (ext) | ||
webtorrent-swarm | webtorrent connection management | ||
webtorrent-tracker | webtorrent tracker server/client |
- compress-sdp (compress sdp messages to lighten load on webtorrent trackers & dht)
- protocol extension: protocol encryption
- protocol extension: µTP
- protocol extension: UPnP and NAT-PMP port forwarding
- protocol extension: webseed support
- webtorrent-dht
WebTorrent is an OPEN Open Source Project. Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit.
WebTorrent is only possible due to the excellent work of the following contributors:
Feross Aboukhadijeh | GitHub/feross | Twitter/@feross |
---|---|---|
Daniel Posch | GitHub/dcposch | Twitter/@dcposch |
John Hiesey | GitHub/jhiesey | Twitter/@jhiesey |
Travis Fischer | GitHub/fisch0920 | Twitter/@fisch0920 |
Astro | GitHub/astro | Twitter/@astro1138 |
Iván Todorovich | GitHub/ivantodorovich | Twitter/@ivantodorovich |
Mathias Buus | GitHub/mafintosh | Twitter/@mafintosh |
Bob Ren | GitHub/bobrenjc93 | Twitter/@bobrenjc93 |
git clone https://github.com/feross/webtorrent.git
cd webtorrent
npm install
./bin/cmd.js --help
In node, enable debug logs by setting the DEBUG
environment variable to the name of the
module you want to debug (e.g. bittorrent-protocol
, or *
to print all logs).
DEBUG=* webtorrent
Of course, this also works for the development version:
DEBUG=* ./bin/cmd.js
In the browser, enable debug logs by running this in the developer console:
localStorage.debug = '*'
Disable by running this:
localStorage.removeItem('debug')
WebTorrent is a modular BitTorrent client, so functionality is split up into many
npm modules. You can git clone
all the relevant dependencies with one command. This
makes it easier to send PRs:
./bin/clone.sh
- Sep 2014 (NodeConf EU) – WebTorrent & WebRTC: Mad Science (first working demo of WebTorrent)
- May 2014 (JS.LA) – How I Built a BitTorrent Client in the Browser (progress update; node client working)
- Oct 2013 (RealtimeConf) – WebRTC Black Magic (RealtimeConf) (where I first shared the idea of WebTorrent)
Chromebooks are set to refuse all incoming connections by default. To change this, run:
sudo iptables -P INPUT ACCEPT
MIT. Copyright (c) Feross Aboukhadijeh.