4

I have some idea for WebTorrent. I have some experience with JavaScript and jQuery, but I never use Node.js or Browserify. Could someone explain me how to use the following simple code?

var client = new WebTorrent()

var torrentId = 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d'

client.add(torrentId, function (torrent) {
  // Torrents can contain many files. Let's use the first.
  var file = torrent.files[0]

  // Display the file by adding it to the DOM. Supports video, audio, image, etc. files
  file.appendTo('body')
})

Should I attach a <script src="webtorrent.min.js"></script> and convert the code from above by Browserify (browserify app.js > bundle.js)? Or maybe I need to use npm install webtorrent and attach some Browserify code?

After using the code npm install webtorrent and just doing Ctrl + c + Ctrl + v, this example from above in <script> tag showed up player. Magic :) But I do something in my VM and now Chrome say that the require('webtorrent') is not definied.

It would be great if I could use WebTorrent as simple as jQuery.

Alireza
  • 2,319
  • 2
  • 23
  • 35
Sokołow
  • 521
  • 5
  • 18

1 Answers1

3

With the code you posted, you don't need to require. If you use the standalone minified version, you can use use var client = WebTorrent()and start using it.

Here's an old example: http://jsfiddle.net/fr090taa/ when I answered this: How to use webtorrent.io

Don't use node/npm/browserify. Just use the minified version.

Check https://github.com/DiegoRBaquero/BTorrent (there's also a clean compiled version to view in JS instead of Coffee)

I'm a WebTorrent project contributor.

Community
  • 1
  • 1
DiegoRBaquero
  • 1,208
  • 10
  • 14
  • Thx. So Webtorrent is very simple in use :) How about Webtorrent-hybrid? There is no standalone version, and I have to use all peers. I must install webtorrent-hybrid in node? Wrtc (unfortunately) is big problem, especially on Azure. – Sokołow Dec 26 '15 at 19:43
  • 1
    webtorrent-hybrid imho is broken. There's nothing bridging the two torrenting networks yet that works as it should. – DiegoRBaquero Dec 30 '15 at 02:04
  • Is there any alternative for bittorrent-hybrid? I would like to use bittorrent, not webtorrent. I do not need a hybrid solution, just bittorrent. – Sokołow Dec 30 '15 at 21:16
  • For the time being, no. The thing is, if you want to use BitTorrent you can, in the browser, only with extensions; not purely as one would like. – DiegoRBaquero Dec 31 '15 at 21:32