Install
To install WebTorrent for use in node or the browser with import WebTorrent from 'webtorrent'
, run:
npm install webtorrent
To install a webtorrent
command line program, run:
npm install webtorrent-cli -g
To install a WebTorrent desktop application for Mac, Windows, or Linux, see WebTorrent Desktop.
Who is using WebTorrent today?
WebTorrent API Documentation
Read the full API Documentation.
In the browser
Downloading a file is simple:
import WebTorrent from 'webtorrent'
const client = new WebTorrent()
const magnetURI = '...'
client.add(magnetURI, torrent => {
// Got torrent metadata!
console.log('Client is downloading:', torrent.infoHash)
for (const file of torrent.files) {
document.body.append(file.name)
}
})
Seeding a file is simple, too:
import dragDrop from 'drag-drop'
import WebTorrent from 'webtorrent'
const client = new WebTorrent()
// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', files => {
client.seed(files, torrent => {
console.log('Client is seeding:', torrent.infoHash)
})
})
There are more examples in docs/get-started.md.
Browserify
WebTorrent works great with browserify, an npm package that lets you use node-style require() to organize your browser code and load modules installed by npm (as seen in the previous examples).
Webpack
WebTorrent also works with webpack, another module bundler. However, webpack requires extra configuration which you can find in the webpack bundle config used by webtorrent.
Or, you can just use the pre-built version via
import WebTorrent from 'webtorrent/dist/webtorrent.min.js'
and skip the webpack configuration.
Script tag
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 type='module'>
import WebTorrent from 'webtorrent.min.js'
</script>
The WebTorrent script is also hosted on fast, reliable CDN infrastructure (Cloudflare and MaxCDN) for easy inclusion on your site:
<script type='module'>
import WebTorrent from 'https://esm.sh/webtorrent'
</script>
Chrome App
If you want to use WebTorrent in a Chrome App, you can include the following script:
<script type='module'>
import WebTorrent from 'webtorrent.chromeapp.js'
</script>
Be sure to enable the chrome.sockets.udp
and chrome.sockets.tcp
permissions!
Talks about WebTorrent
- Sep 2017 - Nordic JS - Get Rich Quick With P2P Crypto Currency
- May 2017 - Char.la - WebTorrent and Peerify (Spanish)
- Nov 2016 - NodeConf Argentina - Real world Electron: Building Cross-platform desktop apps with JavaScript
- May 2016 - SIGNAL Conference - BitTorrent in the Browser
- May 2015 - Data Terra Nemo - WebTorrent: Mother of all demos
- May 2015 - Data Terra Nemo - WebRTC Everywhere
- Nov 2014 - JSConf Asia - How WebTorrent Works
- Sep 2014 - NodeConf EU - WebRTC Mad Science (first working WebTorrent demo)
- Apr 2014 - CraftConf - Bringing BitTorrent to the Web
- May 2014 - JS.LA - How I Built a BitTorrent Client in the Browser (progress update; node client working)
- Oct 2013 - RealtimeConf - WebRTC Black Magic (first mention of idea for WebTorrent)
Modules
Most of the active development is happening inside of small npm packages which are used by WebTorrent.
The Node Way™
“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”
Modules
These are the main modules that make up WebTorrent:
module | tests | version | description |
---|---|---|---|
webtorrent | torrent client (this module) | ||
bittorrent-dht | distributed hash table client | ||
bittorrent-peerid | identify client name/version | ||
bittorrent-protocol | bittorrent protocol stream | ||
bittorrent-tracker | bittorrent tracker server/client | ||
bittorrent-lsd | bittorrent local service discovery | ||
create-torrent | create .torrent files | ||
magnet-uri | parse magnet uris | ||
parse-torrent | parse torrent identifiers | ||
torrent-discovery | find peers via dht, tracker, and lsd | ||
ut_metadata | metadata for magnet uris (protocol extension) | ||
ut_pex | peer discovery (protocol extension) |
Enable debug logs
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
In the browser, enable debug logs by running this in the developer console:
localStorage.setItem('debug', '*')
Disable by running this:
localStorage.removeItem('debug')