WebTorrent Workshop

01 - List the files in a torrent

Write a script that lists the contents of the Sintel torrent. Sintel is a film released as Creative Commons, meaning that it can be freely shared.

Tips

You can create a new torrent client like this:

const client = new WebTorrent()

Next, add the torrent to start the download:

const torrentId = 'https://webtorrent.io/torrents/sintel.torrent'
const torrent = client.add(torrentId)

Wait for the torrent to be ready to use by listening for the 'ready' event. Then print out the name of the torrent, along with a list of all the files in the torrent.

torrent.on('ready', () => {
  console.log('Torrent name:', torrent.name)
  console.log('Files:')
  torrent.files.forEach(file => {
    console.log('- ' + file.name)
  })
})

If you want to learn more about the WebTorrent API, read the docs.

Verify

Open the browser console and confirm that you see the output we printed. If it worked correctly, you should see the following output:

Torrent name: Sintel
Files:
- Sintel.de.srt
- Sintel.en.srt
- Sintel.es.srt
- Sintel.fr.srt
- Sintel.it.srt
- Sintel.mp4
- Sintel.nl.srt
- Sintel.pl.srt
- Sintel.pt.srt
- Sintel.ru.srt
- poster.jpg

If you are stuck, read the solution.

When you are ready, go to the next exercise.