Skip to main content

Overview

The WebTorrent server creates an HTTP server to serve the contents of torrents, dynamically fetching needed pieces to satisfy requests. Range requests are supported.
WebTorrent provides two server implementations:
  • NodeServer for Node.js environments
  • BrowserServer for browser environments (requires Service Worker)

Creating a Server

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

Creates an HTTP server for serving torrent contents. See the WebTorrent API for more details on client methods.

Node.js Server

Configuration Options

origin
string
default:"*"
Allow requests from specific origin. Set to false for same-origin only.
hostname
string
If specified, only allow requests whose Host header matches this hostname.
path
string
default:"/webtorrent"
Base path for the server.

Usage Example

import WebTorrent from 'webtorrent'

const client = new WebTorrent()
const server = client.createServer({
  hostname: 'localhost',
  origin: 'http://localhost:3000'
})

// Start listening on a port
server.server.listen(3000, () => {
  console.log('Server listening on port 3000')
})

client.add(magnetURI, torrent => {
  console.log('Torrent added')
  
  // Access files via HTTP
  const url = torrent.files[0].streamURL
  console.log('Stream URL:', url)
  // http://localhost:3000/webtorrent/<infoHash>/filename.mp4
})

// Later, cleanup
server.close()
client.destroy()

URL Structure

The server provides the following endpoints:
/
GET
Root endpoint (no content displayed)
/webtorrent/
GET
Lists all torrents with links to their detail pages
/webtorrent/:infoHash
GET
Lists all files in the torrent with links to stream/download them
/webtorrent/:infoHash/:filepath
GET
Streams or downloads the specific file

Methods

server.server.listen(port, [callback])

Start the server listening on the specified port.
server.server.listen(8080, () => {
  console.log('Server running on port 8080')
})

server.close([callback])

Close the server and stop accepting new requests.
server.close(err => {
  if (err) throw err
  console.log('Server closed')
})

server.destroy([callback])

Destroy the server and close all open connections.
server.destroy(err => {
  if (err) throw err
  console.log('Server destroyed')
})

server.address()

Get the server’s address information.
return
object
Object with address, family, and port properties
const addr = server.address()
console.log(`Server at ${addr.address}:${addr.port}`)

Browser Server

Configuration Options

controller
ServiceWorkerRegistration
required
Service worker registration
origin
string
default:"*"
Allow requests from specific origin. Set to false for same-origin only.

Usage Example

import WebTorrent from 'webtorrent'

const client = new WebTorrent()

// Register service worker
const controller = await navigator.serviceWorker.register('./sw.min.js', {
  scope: './'
})

// Wait for service worker to be ready
await navigator.serviceWorker.ready

// Create server
client.createServer({ controller })

client.add(magnetURI, torrent => {
  const video = document.querySelector('video')
  
  // Stream to video element
  const file = torrent.files[0]
  file.streamTo(video)
  
  // Or use the URL directly
  console.log('Stream URL:', file.streamURL)
  // /webtorrent/<infoHash>/filename.mp4
})

// Later, cleanup
client._server.close()
client.destroy()

Service Worker

You can use the official WebTorrent service worker or implement the required functionality yourself:
// sw.js - Service Worker implementation
self.addEventListener('fetch', event => {
  // Handle WebTorrent streaming requests
  // See lib/worker.js in WebTorrent source for reference
})

Methods

server.close([callback])

Close the server.
client._server.close(err => {
  if (err) throw err
  console.log('Browser server closed')
})

server.destroy([callback])

Destroy the server.
client._server.destroy(err => {
  if (err) throw err
  console.log('Browser server destroyed')
})

Response Headers

All file responses include the following headers:
Accept-Ranges
string
Indicates support for range requests
Content-Type
string
MIME type of the file
Content-Length
number
Size of the response body in bytes
Content-Range
string
Present for partial content responses (206)
Cache-Control
string
Disables caching (data is local)
Access-Control-Allow-Origin
string
CORS header (if origin is allowed)

CORS Support

By default, the server allows all origins (origin: '*'). You can restrict this:
// Allow specific origin
client.createServer({
  origin: 'https://example.com'
})

// Disable cross-origin requests
client.createServer({
  origin: false
})

Range Requests

The server supports HTTP range requests, enabling:
  • Video/audio seeking
  • Resumable downloads
  • Partial content delivery
curl -H "Range: bytes=0-1023" \
  http://localhost:3000/webtorrent/<infoHash>/video.mp4

DLNA Support

The server includes DLNA streaming headers for compatibility with DLNA devices:
transferMode.dlna.org: Streaming
contentFeatures.dlna.org: DLNA.ORG_OP=01;...