Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The `webtorrent` module should work with browserify #88

Closed
feross opened this issue Sep 13, 2014 · 4 comments
Closed

The `webtorrent` module should work with browserify #88

feross opened this issue Sep 13, 2014 · 4 comments

Comments

@feross
Copy link
Member

@feross feross commented Sep 13, 2014

To use WebTorrent right now, you need to use bittorrent-client, the module which webtorrent uses internally. This is because webtorrent is currently doing a bunch of node-specific things like reading from the filesystem and starting an http server.

Let's use the browser field spec to exclude these files so people can just do require('webtorrent')

@feross feross closed this in 8290a91 Sep 13, 2014
@feross

This comment has been minimized.

Copy link
Member Author

@feross feross commented Sep 13, 2014

This is fixed as of webtorrent 0.6.0. Now require('webtorrent') will work in the browser!

Here's an example of how to use WebTorrent:

var dragDrop = require('drag-drop/buffer')
var WebTorrent = require('webtorrent')

var client = new WebTorrent()

// when user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', function (files) {
  client.seed(files, onTorrent)
})

function onTorrent (torrent) {
  console.log('Torrent info hash: ' + torrent.infoHash)

  // go through each file in the torrent, create a link to it, and add it to the DOM
  torrent.files.forEach(function (file) {
    file.createReadStream().pipe(concat(function (buf) {
      var a = document.createElement('a')
      a.download = file.name
      a.href = URL.createObjectURL(new Blob([ buf ]))
      a.textContent = 'download ' + file.name
      document.body.appendChild(a)
    }))
  })
}

// call this function to download a torrent!
function download (infoHash) {
  client.download({
    infoHash: infoHash,
    announce: [ 'wss://tracker.webtorrent.io' ]
  }, onTorrent)
}
@feross

This comment has been minimized.

Copy link
Member Author

@feross feross commented Sep 13, 2014

Added this example code to the main readme!

@rom1504

This comment has been minimized.

Copy link
Member

@rom1504 rom1504 commented Sep 13, 2014

Nice !
I guess "webtorrent.js (bittorrent client for the browser -- coming soon)" is not needed anymore ?
Now there's just the streaming in the html video tag missing and torrent streaming in the browser is done, right ?

@feross

This comment has been minimized.

Copy link
Member Author

@feross feross commented Sep 17, 2014

Yep! Updated the readme.

@lock lock bot locked as resolved and limited conversation to collaborators May 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.