Skip to main content

Constructor

new WebTorrent([opts])

Create a new WebTorrent instance.
opts
object
Client configuration options
const client = new WebTorrent({
  maxConns: 100,
  downloadLimit: 1000000, // 1 MB/s
  uploadLimit: 500000     // 500 KB/s
})

Static Properties

WebTorrent.WEBRTC_SUPPORT

Boolean indicating whether WebRTC is natively supported in the current environment.
if (WebTorrent.WEBRTC_SUPPORT) {
  // WebRTC is supported
} else {
  // Use a fallback
}

WebTorrent.VERSION

String representing the current WebTorrent version.

Methods

client.add(torrentId, [opts], [ontorrent])

Start downloading a new torrent.
torrentId
string | Uint8Array | object
required
Can be:
  • Magnet URI (string)
  • Torrent file (Uint8Array)
  • Info hash (hex string or Uint8Array)
  • Parsed torrent object
  • HTTP/HTTPS URL to a torrent file (string)
  • Filesystem path to a torrent file (Node.js only)
opts
object
Torrent-specific options. See options page for details.
ontorrent
function
Callback called when torrent is ready (has metadata)
return
Torrent
Returns the torrent instance immediately (before metadata is available)
const torrent = client.add('magnet:?xt=urn:btih:...', torrent => {
  console.log('Torrent ready:', torrent.name)
  torrent.files.forEach(file => {
    console.log('File:', file.name)
  })
})

client.seed(input, [opts], [onseed])

Start seeding a new torrent.
input
various
required
Can be:
  • Filesystem path to file or folder (string, Node.js only)
  • W3C FileList object (browser only)
  • W3C File/Blob object
  • Typed array or array of numbers
  • Node Buffer object
  • Node Readable stream object
  • Array of any of the above
opts
object
Options for create-torrent and client.add combined
onseed
function
Called when client has begun seeding
return
Torrent
Returns the torrent instance immediately
// Seed a file in the browser
const file = document.querySelector('input[type=file]').files[0]
client.seed(file, torrent => {
  console.log('Seeding:', torrent.magnetURI)
})
// Seed a file in Node.js
client.seed('/path/to/file.mp4', torrent => {
  console.log('Seeding:', torrent.magnetURI)
})

client.remove(torrentId, [opts], [callback])

Remove a torrent from the client. Destroy all connections to peers and delete all saved file metadata.
torrentId
string | Buffer | Torrent
required
Torrent to remove
opts
object
callback
function
Called when torrent is fully destroyed
await client.remove(torrentId)
console.log('Torrent removed')

client.get(torrentId)

Returns the torrent with the given torrentId.
torrentId
string | Buffer | object | Torrent
required
Torrent identifier
return
Promise<Torrent | null>
Promise that resolves to the torrent, or null if not found
const torrent = await client.get(infoHash)
if (torrent) {
  console.log('Found torrent:', torrent.name)
}

client.destroy([callback])

Destroy the client, including all torrents and connections to peers.
callback
function
Called when client has gracefully closed
client.destroy(err => {
  if (err) throw err
  console.log('Client destroyed')
})

client.createServer([opts], [force])

Create an HTTP server to serve torrent contents.
opts
object
force
'browser' | 'node'
Force specific implementation
return
NodeServer | BrowserServer
Server instance
// Node.js
const instance = client.createServer()
instance.server.listen(3000)

// Browser
const controller = await navigator.serviceWorker.register('./sw.min.js')
await navigator.serviceWorker.ready
client.createServer({ controller })

client.throttleDownload(rate)

Sets the maximum download speed.
rate
number
required
Speed in bytes/sec. Use -1 to disable throttling.
client.throttleDownload(1000000) // Limit to 1 MB/s

client.throttleUpload(rate)

Sets the maximum upload speed.
rate
number
required
Speed in bytes/sec. Use -1 to disable throttling.
client.throttleUpload(500000) // Limit to 500 KB/s

client.address()

Returns the address the client is listening on.
return
object | null
Address object with address, family, and port properties

Properties

client.torrents

torrents
Array<Torrent>
Array of all torrents in the client

client.peerId

peerId
string
Client peer ID (hex string)

client.downloadSpeed

downloadSpeed
number
Total download speed for all torrents, in bytes/sec

client.uploadSpeed

uploadSpeed
number
Total upload speed for all torrents, in bytes/sec

client.progress

progress
number
Total download progress for all active torrents, from 0 to 1

client.ratio

ratio
number
Aggregate seed ratio for all torrents (uploaded / downloaded)

Events

See the Events reference for all client events.