Get started with webtorrent-component CDN

Apache-2.0 licensed

Webtorrent-component is a library for creating BitTorrent clients in the browser using WebRTC.

Tags:
  • html
  • element
  • webtorrent
  • torrent

Stable version

Copied!

How to start using webtorrent-component CDN


<!DOCTYPE html>
<html>
<head>
  <title>Get started with webtorrent-component CDN - cdnhub.io</title>
  <script src="https://cdn.jsdelivr.net/npm/peer/dist/peer.min.js"></script>
  <script src="https://cdn.cdnhub.io/webtorrent-component/1.0.2/webtorrent-component.min.js"></script>
</head>
<body>
  <button id="download">Download</button>
  <script>
    const peer = new Peer();
    const downloadButton = document.getElementById('download');

    downloadButton.addEventListener('click', () => {
      const webtorrent = new PeerWebtorrent(peer);
      const torrent = webtorrent.add('https://example.com/torrent.torrent');

      torrent.on('downloadProgress', (progress) => {
        console.log(`Downloaded ${progress.received} bytes, total ${progress.length}`);
      });

      torrent.on('error', (error) => {
        console.error('Error downloading torrent:', error);
      });

      torrent.on('warning', (warning) => {
        console.warn('Warning downloading torrent:', warning);
      });

      torrent.on('peerConnected', (peer) => {
        console.log('Connected to peer:', peer.id);
      });

      torrent.on('peerDisconnected', (peer) => {
        console.log('Disconnected from peer:', peer.id);
      });

      torrent.on('bitfieldComplete', () => {
        console.log('Bitfield complete');
      });

      torrent.on('pieceLoaded', (piece, index) => {
        console.log(`Loaded piece ${index} of ${torrent.pieces.length}`);
      });

      torrent.on('pieceError', (piece, error) => {
        console.error(`Error loading piece ${piece.index}: ${error}`);
      });

      torrent.on('pieceWarning', (piece, warning) => {
        console.warn(`Warning loading piece ${piece.index}: ${warning}`);
      });

      torrent.on('announceSuccess', (infoHash, peerId, announce) => {
        console.log('Announce success:', infoHash, peerId, announce);
      });

      torrent.on('announceFailure', (error) => {
        console.error('Announce failure:', error);
      });

      torrent.on('magnetAnnounceSuccess', (infoHash, response) => {
        console.log('Magnet announce success:', infoHash, response);
      });

      torrent.on('magnetAnnounceFailure', (error) => {
        console.error('Magnet announce failure:', error);
      });

      torrent.on('ready', () => {
        console.log('Torrent ready');
      });

      torrent.on('error', (error) => {
        console.error('Torrent error:', error);
      });

      torrent.on('warning', (warning) => {
        console.warn('Torrent warning:', warning);
      });

      torrent.on('close', () => {
        console.log('Torrent closed');
      });
    });
  </script>
</body>
</html>
Copied!
Copied!

All versions