webtorrent / webtorrent

⚡️ Streaming torrent client for the web

Home Page:https://webtorrent.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: client.createServer is not a function

hirios opened this issue · comments

What version of this package are you using?
webtorrent.min.js from github

What operating system, Node.js, and npm version?
Window 10,
Node v20.4.0
NPM 9.7.2
Chrome 114.0.5735.199

What happened?
I'm trying to run the webtorrent in the browser using the webtorrent.min.js file. I'm using live server to run on my http://localhost.

<script src="webtorrent.min.js"></script>

but I get the error client.createServer is not a function.

my project folder has the following files sw.min.js, sw.min.js.map, webtorrent.min.js, webtorrent.min.js.map

Here is the index.html

<!DOCTYPE html>
<html>
<head>
  <title>new app</title>
</head>
<script src="webtorrent.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/video.js/7.8.1/video.min.js"></script>

<body>
  <div>
    <video width="800" id="video-container" class="video-js" data-setup="{}" controls="true"></video>
  </div>
  <script>
    var torrentId = 'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent'
   
    const player = document.querySelector('video')
    const client = new WebTorrent()

    function download() {
      client.add(torrentId, torrent => {
        // Torrents can contain many files. Let's use the .mp4 file
        const file = torrent.files.find(file => file.name.endsWith('.mp4'))

        // Stream to a <video> element by providing an the DOM element
        file.streamTo(player)
        console.log('Ready to play!')
      })
    }

    navigator.serviceWorker.register('./sw.min.js', { scope: './' }).then(reg => {
      const worker = reg.active || reg.waiting || reg.installing

      console.log('worker')
      function checkState(worker) {
        return worker.state === 'activated' && client.createServer({ controller: reg }) && download()
      }

      if (!checkState(worker)) {
        worker.addEventListener('statechange', ({ target }) => checkState(target))
      }
    })
  </script>
</body>
</html>

I'm also encountering this. Any ideas?

@hirios it appears the createServer function is defined on the Torrent class in the 2.0? Just found that from here #2613

his issue is different, he's running it on client not on torrent, so I have no clue whats wrong in his case

@liamzebedee @ThaUnknown

I got it that way. I hadn't tried anything else either, torrents only work if they are of the webrtc type

https://github.com/hirios/webtorrent-example

use 'https://esm.sh/webtorrent' not jsdelivr

I got the same error with webtorrent 2.1.25 using TypeScript and webpack.
I'm importing WebTorrent this way as stated in the documentation :

import WebTorrent from 'webtorrent/dist/webtorrent.min.js'

Am I doing something wrong ?

I tried a very simple example :

<!DOCTYPE html>
<html>
  <head>
    <title>new app</title>
  </head>
  <script src="webtorrent.min.js"></script>
  <body>
    <script>
      var torrentId =
        "magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent";

      const client = new WebTorrent();

      client.add(torrentId, (torrent) => {
        console.log("Ready to play!", torrent);
        torrent.on("ready", () => {
          console.log("Creating server");
          torrent.createServer();
        });
      });
    </script>
  </body>
</html>

torrent.createServer does not exist.

EDIT : didn't read the #2613 issue, I understand now :).