← Back to webtorrent/webtorrent

How to Deploy & Use webtorrent/webtorrent

WebTorrent Deployment and Usage Guide

Prerequisites

  • Node.js (version 14 or higher)
  • npm (version 6 or higher)
  • Git for cloning the repository
  • WebRTC-compatible browser (Chrome, Firefox, Opera, or Safari) for browser usage

Installation

For Development

  1. Clone the repository:

    git clone https://github.com/webtorrent/webtorrent.git
    cd webtorrent
    
  2. Install dependencies:

    npm install
    

For Production Use

Install WebTorrent as a dependency in your project:

npm install webtorrent

For Command Line Usage

Install the WebTorrent CLI globally:

npm install webtorrent-cli -g

For Browser Usage

Include the minified script directly in your HTML:

<script src="https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js"></script>

Or use a module bundler like Browserify or Webpack:

npm install webtorrent

Configuration

Environment Variables

WebTorrent doesn't require specific environment variables for basic operation. However, you can configure:

  • Peer ID: Set a custom peer ID by passing it in the options when creating a WebTorrent client:

    const client = new WebTorrent({ peerId: 'your-custom-peer-id' })
    
  • Port Configuration: The client automatically selects ports, but you can configure torrent ports:

    const client = new WebTorrent({ torrentPort: 6881 })
    

Browser Configuration

For browser usage, ensure your environment supports WebRTC. WebTorrent works in Chrome, Firefox, Opera, and Safari without plugins.

Build & Run

Development

  1. Start the development server:

    npm run dev
    
  2. Run tests:

    npm test
    

Production

For production usage in Node.js applications:

const WebTorrent = require('webtorrent')
const client = new WebTorrent()

// Add a torrent via magnet link or torrent file
client.add('magnet:?xt=...', (torrent) => {
  console.log('Torrent ready')
})

For browser applications:

<script src="https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js"></script>
<script>
  const client = new WebTorrent()
  client.add('magnet:?xt=...', (torrent) => {
    console.log('Torrent ready')
  })
</script>

Deployment

Node.js Applications

Deploy your WebTorrent application to any Node.js hosting platform:

  • Vercel (for serverless functions)
  • Railway (for full Node.js applications)
  • Heroku (traditional Node.js deployment)
  • DigitalOcean App Platform

Example deployment to Railway:

  1. Install Railway CLI:

    npm install -g @railway/cli
    
  2. Initialize Railway project:

    railway init
    
  3. Deploy:

    railway up
    

Browser Applications

For browser-based WebTorrent applications, deploy to any static hosting service:

  • Vercel
  • Netlify
  • GitHub Pages
  • Cloudflare Pages

Desktop Applications

For desktop applications using WebTorrent Desktop, download pre-built binaries from the WebTorrent Desktop repository.

Troubleshooting

Common Issues

  1. WebRTC Not Supported

    • Ensure you're using a WebRTC-compatible browser
    • Check browser console for WebRTC errors
    • Verify that WebRTC is not blocked by extensions or network policies
  2. Connection Issues

    • Check firewall settings that might block torrent ports (6881-6889)
    • Verify that your network allows P2P connections
    • For browser usage, ensure you're connecting to WebTorrent-compatible peers
  3. Performance Issues

    • Use a wired connection instead of WiFi when possible
    • Limit the number of concurrent torrents if experiencing slowdowns
    • Check for bandwidth throttling by your ISP
  4. Installation Issues

    • Ensure you have the correct Node.js version (14+)
    • Clear npm cache if installation fails:
      npm cache clean --force
      npm install
      
  5. Browser Compatibility

    • WebTorrent works in Chrome, Firefox, Opera, and Safari
    • Mobile browsers may have limited support
    • Test in multiple browsers if you encounter issues

Debug Mode

Enable debug logging to troubleshoot issues:

DEBUG=webtorrent:* npm test

Or in browser:

localStorage.debug = 'webtorrent:*'

Getting Help

Security Considerations

  • WebTorrent uses WebRTC which is generally secure, but always validate torrent sources
  • For production applications, consider implementing rate limiting and user authentication
  • Be aware of potential copyright issues when downloading torrents