GithubHelp home page GithubHelp logo

webtorrent / torrent-discovery Goto Github PK

View Code? Open in Web Editor NEW
215.0 15.0 65.0 214 KB

Discover BitTorrent and WebTorrent peers

Home Page: https://webtorrent.io

License: MIT License

JavaScript 100.00%
bittorrent webtorrent javascript nodejs browser peer p2p torrent tracker dht

torrent-discovery's Introduction


WebTorrent
WebTorrent

The streaming torrent client. For node.js and the web.

discord ci npm version npm downloads Standard - JavaScript Style Guide

Sponsored by    Socket - JavaScript open source supply chain security    Wormhole

WebTorrent is a streaming torrent client for node.js and the browser. YEP, THAT'S RIGHT. THE BROWSER. It's written completely in JavaScript – the language of the web – so the same code works in both runtimes.

In node.js, this module is a simple torrent client, using TCP and UDP to talk to other torrent clients.

In the browser, WebTorrent uses WebRTC (data channels) for peer-to-peer transport. It can be used without browser plugins, extensions, or installations. It's Just JavaScript™. Note: WebTorrent does not support UDP/TCP peers in browser.

Simply include the webtorrent.min.js script on your page to start fetching files over WebRTC using the BitTorrent protocol, or import WebTorrent from 'webtorrent' with browserify or webpack. See demo apps and code examples below.

jsdelivr download count

To make BitTorrent work over WebRTC (which is the only P2P transport that works on the web) we made some protocol changes. Therefore, a browser-based WebTorrent client or "web peer" can only connect to other clients that support WebTorrent/WebRTC.

To seed files to web peers, use a client that supports WebTorrent, e.g. WebTorrent Desktop, a desktop client with a familiar UI that can connect to web peers, webtorrent-hybrid, a command line program, or Instant.io, a website. Established torrent clients like Vuze have already added WebTorrent support so they can connect to both normal and web peers. We hope other clients will follow.

Network

Features

  • Torrent client for node.js & the browser (same npm package!)
  • Insanely fast
  • Download multiple torrents simultaneously, efficiently
  • Pure Javascript (no native dependencies)
  • Exposes files as streams
    • Fetches pieces from the network on-demand so seeking is supported (even before torrent is finished)
    • Seamlessly switches between sequential and rarest-first piece selection strategy
  • Supports advanced torrent client features
  • Comprehensive test suite (runs completely offline, so it's reliable and fast)
  • Check all the supported BEPs here

Browser/WebRTC environment features

  • WebRTC data channels for lightweight peer-to-peer communication with no plugins
  • No silos. WebTorrent is a P2P network for the entire web. WebTorrent clients running on one domain can connect to clients on any other domain.
  • Stream video torrents into a <video> tag (webm, mkv, mp4, ogv, mov, etc (AV1, H264, HEVC*, VP8, VP9, AAC, FLAC, MP3, OPUS, Vorbis, etc))
  • Supports Chrome, Firefox, Opera and Safari.

Install

To install WebTorrent for use in node or the browser with import WebTorrent from 'webtorrent', run:

npm install webtorrent

To install a webtorrent command line program, run:

npm install webtorrent-cli -g

To install a WebTorrent desktop application for Mac, Windows, or Linux, see WebTorrent Desktop.

Ways to help

Who is using WebTorrent today?

Lots of folks!

WebTorrent API Documentation

Read the full API Documentation.

Usage

WebTorrent is the first BitTorrent client that works in the browser, using open web standards (no plugins, just HTML5 and WebRTC)! It's easy to get started!

In the browser

Downloading a file is simple:
import WebTorrent from 'webtorrent'

const client = new WebTorrent()
const magnetURI = '...'

client.add(magnetURI, torrent => {
  // Got torrent metadata!
  console.log('Client is downloading:', torrent.infoHash)

  for (const file of torrent.files) {
    document.body.append(file.name)
  }
})
Seeding a file is simple, too:
import dragDrop from 'drag-drop'
import WebTorrent from 'webtorrent'

const client = new WebTorrent()

// When user drops files on the browser, create a new torrent and start seeding it!
dragDrop('body', files => {
  client.seed(files, torrent => {
    console.log('Client is seeding:', torrent.infoHash)
  })
})

There are more examples in docs/get-started.md.

Browserify

WebTorrent works great with browserify, an npm package that lets you use node-style require() to organize your browser code and load modules installed by npm (as seen in the previous examples).

Webpack

WebTorrent also works with webpack, another module bundler. However, webpack requires extra configuration which you can find in the webpack bundle config used by webtorrent.

Or, you can just use the pre-built version via import WebTorrent from 'webtorrent/dist/webtorrent.min.js' and skip the webpack configuration.

Script tag

WebTorrent is also available as a standalone script (webtorrent.min.js) which exposes WebTorrent on the window object, so it can be used with just a script tag:

<script type='module'>
  import WebTorrent from 'webtorrent.min.js'
</script>

The WebTorrent script is also hosted on fast, reliable CDN infrastructure (Cloudflare and MaxCDN) for easy inclusion on your site:

<script type='module'>
  import WebTorrent from 'https://esm.sh/webtorrent'
</script>
Chrome App

If you want to use WebTorrent in a Chrome App, you can include the following script:

<script type='module'>
  import WebTorrent from 'webtorrent.chromeapp.js'
</script>

Be sure to enable the chrome.sockets.udp and chrome.sockets.tcp permissions!

In Node.js

WebTorrent also works in node.js, using the same npm package! It's mad science!

NOTE: To connect to "web peers" (browsers) in addition to normal BitTorrent peers, use webtorrent-hybrid which includes WebRTC support for node.

As a command line app

WebTorrent is also available as a command line app. Here's how to use it:

$ npm install webtorrent-cli -g
$ webtorrent --help

To download a torrent:

$ webtorrent magnet_uri

To stream a torrent to a device like AirPlay or Chromecast, just pass a flag:

$ webtorrent magnet_uri --airplay

There are many supported streaming options:

--airplay               Apple TV
--chromecast            Chromecast
--mplayer               MPlayer
--mpv                   MPV
--omx [jack]            omx [default: hdmi]
--vlc                   VLC
--xbmc                  XBMC
--stdout                standard out [implies --quiet]

In addition to magnet uris, WebTorrent supports many ways to specify a torrent.

Talks about WebTorrent

Modules

Most of the active development is happening inside of small npm packages which are used by WebTorrent.

The Node Way™

"When applications are done well, they are just the really application-specific, brackish residue that can't be so easily abstracted away. All the nice, reusable components sublimate away onto github and npm where everybody can collaborate to advance the commons." — substack from "how I write modules"

node.js is shiny

Modules

These are the main modules that make up WebTorrent:

module tests version description
webtorrent torrent client (this module)
bittorrent-dht distributed hash table client
bittorrent-peerid identify client name/version
bittorrent-protocol bittorrent protocol stream
bittorrent-tracker bittorrent tracker server/client
bittorrent-lsd bittorrent local service discovery
create-torrent create .torrent files
magnet-uri parse magnet uris
parse-torrent parse torrent identifiers
torrent-discovery find peers via dht, tracker, and lsd
ut_metadata metadata for magnet uris (protocol extension)
ut_pex peer discovery (protocol extension)

Enable debug logs

In node, enable debug logs by setting the DEBUG environment variable to the name of the module you want to debug (e.g. bittorrent-protocol, or * to print all logs).

DEBUG=* webtorrent

In the browser, enable debug logs by running this in the developer console:

localStorage.setItem('debug', '*')

Disable by running this:

localStorage.removeItem('debug')

License

MIT. Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC.

torrent-discovery's People

Contributors

artskydj avatar bricewge avatar dcposch avatar diegorbaquero avatar feross avatar greenkeeper[bot] avatar greenkeeperio-bot avatar hicom150 avatar jimmywarting avatar programmerben avatar renovate-bot avatar renovate[bot] avatar semantic-release-bot avatar sidd avatar thaunknown avatar zunsthy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

torrent-discovery's Issues

Does torrent-discovery support local discovery?

I'm trying to have two peers discover each other on the local network using node.js. Here's the code:

test.js

const SimplePeer = require('simple-peer');
const wrtc = require('wrtc');
const Discovery = require('torrent-discovery');

p2p_port = process.env.P2P_PORT || 6001;

const peer1 = new SimplePeer({ initiator: true, wrtc: wrtc });

const discovery = new Discovery({
  infoHash: 'test',
  peerId: p2p_port,
  port: p2p_port
})

discovery.on('peer', (peer2, source) => {
  console.log('found peer!', peer2);

  peer1.on('signal', function (data) {
    // when peer1 has signaling data, give it to peer2 somehow
    peer2.signal(data)
  })

  peer2.on('signal', function (data) {
    // when peer2 has signaling data, give it to peer1 somehow
    peer1.signal(data)
  })

  peer1.on('connect', function () {
    // wait for 'connect' event before using the data channel
    peer1.send('hey peer2, how is it going?')
  })

  peer2.on('data', function (data) {
    // got a data channel message
    console.log('got a message from peer1: ' + data)
  })
})

discovery.on('dhtAnnounce', () => {
  console.log('dht announced!');
})

discovery.on('warning', (err) => {
  console.log('warning!', err);
})

discovery.on('peer', (err) => {
  console.log('error!', err)
})

Then I run these two commands:
P2P_PORT=6001 node test.js
P2P_PORT=6002 node test.js

They don't seem to discover each other. What am I doing wrong?

hi is therer a way to further enlarge discovery scope? discovery seems limited

pls try this link magnet:?xt=urn:btih:C640A3DD15F880EED6DF1BE0094DCF8A937EE434 . on my computer it prints out

connecting to 45.32.63.60:22299
get peer
45.32.63.60:22299
get peer
113.109.223.114:10453
connecting to 113.109.223.114:10453
get peer
45.32.63.60:6999
connecting to 45.32.63.60:6999
get peer
45.32.63.60:6999
get peer
173.68.9.69:11861
connecting to 173.68.9.69:11861
get peer
183.37.116.133:28562
connecting to 183.37.116.133:28562
get peer
45.32.63.60:6999
get peer
45.32.63.60:6999
get peer
45.32.63.60:6999
get peer
113.109.223.114:10453
get peer
173.68.9.69:11861
get peer
183.37.116.133:28562
get peer
183.37.116.133:28562
get peer
45.32.63.60:6999
get peer
113.109.223.114:10453
get peer
45.32.63.60:6999
get peer
113.109.223.114:10453
get peer
45.32.63.60:6999
get peer
45.32.63.60:6999
get peer
113.109.223.114:10453
get peer
113.109.223.114:10453

getting about 4 peers while on one of my p2p download software it dispays about 18 peers . is there still ways to get better?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/release.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/cache v3
npm
package.json
  • bittorrent-dht ^11.0.5
  • bittorrent-tracker ^11.0.2
  • bittorrent-lsd ^2.0.0
  • debug ^4.3.4
  • run-parallel ^1.2.0
  • @webtorrent/semantic-release-config 1.0.10
  • randombytes 2.1.0
  • semantic-release 21.1.2
  • standard *
  • tape 5.7.5

  • Check this box to trigger a request for Renovate to run again on this repository

using the discovery without port specified

is the port required even for dht? I want to use it without the tracker.
port: 0 // torrent client port (only required in node), why for node only?
In node, peer is a string in the form 12:34:56:78:4000 why, any additional info?
thanks.

Support local service discovery BEP 14

I've been having trouble getting clients to seed to each other either on the same machine or on the same network (on a home network).

Another concern is that the reliance on the DHT / Trackers means that you can't discover peers if you're not connected to the internet at large.

Would a PR that adds support for BEP 14 be welcome?

No public API to announce `complete` event to tracker

I think there should be an API to send the complete event to the tracker.

It's of course possible to do so using discovery.tracker.complete() but either a specific complete() function should be added or the tracker field should be documented

Error [ERR_PACKAG_PATH_NOT_EXPORTED]

What version of this package are you using?
10.0.13
Node.js v20.3.1
What operating system, Node.js, and npm version?
MacOS 13.4.1 (22F82)
What happened?
node:internal/modules/cjs/loader:577
throw e;
^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /...node_modules/torrent-discovery/package.json
at new NodeError (node:internal/errors:405:5)
at exportsNotFound (node:internal/modules/esm/resolve:259:10)
at packageExportsResolve (node:internal/modules/esm/resolve:533:13)
at resolveExports (node:internal/modules/cjs/loader:571:36)
at Module._findPath (node:internal/modules/cjs/loader:645:31)
at Module._resolveFilename (node:internal/modules/cjs/loader:1058:27)
at Module._load (node:internal/modules/cjs/loader:925:27)
at Module.require (node:internal/modules/cjs/loader:1139:19)
at require (node:internal/modules/helpers:121:18)
at Object.
{
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v20.3.1
What did you expect to happen?
The library should work.
Are you willing to submit a pull request to fix this bug?
If I can figure out why this is happening

How to setting intervalMs for discovery

Hi feross!

I'm studing your code to apply for my project. I have a question for you: client use interval to request to tracker server, but how to setting the time for interval?

I see that when torrent call new Discovery, the attribute intervalMs is not define.
In Discovery :

self._intervalMs = opts.intervalMs || (15 * 60 * 1000)

In Torrent :

self.discovery = new Discovery({
    infoHash: self.infoHash,
    announce: self.announce,
    peerId: self.client.peerId,
    dht: !self.private && self.client.dht,
    tracker: trackerOpts,
    port: self.client.torrentPort
  })

Please check help me.
Thanks!

Should periodically reannounce to the DHT

This is the package where it makes sense to handle this.

bittorrent-tracker handles reannouncing to the trackers, since they can request a specific re-announce interval. It defaults to 15 minutes.

bittorrent-dht is unopionated and doesn't handle re-announcing. Let's handle it here.

cc @mafintosh

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because we are using your CI build statuses to figure out when to notify you about breaking changes.

Since we did not receive a CI status on the greenkeeper/initial branch, we assume that you still need to configure it.

If you have already set up a CI for this repository, you might need to check your configuration. Make sure it will run on all new branches. If you don’t want it to run on every branch, you can whitelist branches starting with greenkeeper/.

We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

Once you have installed CI on this repository, you’ll need to re-trigger Greenkeeper’s initial Pull Request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Does it discover seeds continuasly?

Hey, I saw you switched peer-discovery.js with this module in torrent-stream.

I had a fork of torrent-stream that used a method for a not-so-common occurrence in which a poorly seeded torrent gets all the seeds blacklisted by peer-wire-swarm for various reasons until none are left.

The only solution I found for this was to create a method that forces reconnecting to all known peers and also restarts the peer discovery in peer-discovery.js. Restarting peer discovery was important too as from various tests it proved that peer-discovery.js stopped searching for new peers for some reason, and in poorly seeded torrents and new torrents (in which seeds may be few but their numbers increase fast), rediscovery of peers is a must.

I'm curious if restarting peer discovery would still be needed with torrent-discovery or not.

Seeds are not discovered

Hi @feross ! Please, can you tell me why seeds aren't discovered when i'm using torrent-discovery?
I made a comparison of discovered addresses with utorrent client's seeds and found, that there are no matches in this lists. Could you advice me what i'm doing wrong?

hi is therer a way to further enlarge discovery scope?

hey the problem is not about a specific link, it happens with many other links as well. I posed the issue just to see if there could be some improvement made about the project. Any ideas how to make more peer discovery ?

Discovery peers warning/error

After event dhtAnnounce i always get the same warning(error?):
Error: invalid message d1:a��� from <ip>:<port> Invalid data: Missing delimiter ":" [0x3a]

Any ideas?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.