|
| 1 | +# bittorrent-tracker [](https://travis-ci.org/feross/bittorrent-tracker) [](https://npmjs.org/package/bittorrent-tracker) [](https://www.gittip.com/feross/) |
| 2 | + |
| 3 | +### Simple, robust, BitTorrent tracker (client & server) implementation |
| 4 | + |
| 5 | +Node.js implementation of a [BitTorrent tracker](https://wiki.theory.org/BitTorrentSpecification#Tracker_HTTP.2FHTTPS_Protocol), client and server. |
| 6 | + |
| 7 | +A **BitTorrent tracker** is an HTTP service which responds to GET requests from BitTorrent |
| 8 | +clients. The requests include metrics from clients that help the tracker keep overall |
| 9 | +statistics about the torrent. The response includes a peer list that helps the client |
| 10 | +participate in the torrent. |
| 11 | + |
| 12 | +Also see [BitTorrent DHT](https://github.com/feross/bittorrent-dht). This module is used |
| 13 | +by [WebTorrent](http://webtorrent.io). |
| 14 | + |
| 15 | +## install |
| 16 | + |
| 17 | +``` |
| 18 | +npm install bittorrent-tracker |
| 19 | +``` |
| 20 | + |
| 21 | +## usage |
| 22 | + |
| 23 | +To connect to a tracker, just do this: |
| 24 | + |
| 25 | +```js |
| 26 | +var Client = require('bittorrent-tracker').Client |
| 27 | +var parseTorrent = require('parse-torrent') |
| 28 | + |
| 29 | +var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent') |
| 30 | +var parsedTorrent = parseTorrent(torrent) // { infoHash: 'xxx', length: xx, announce: ['xx', 'xx'] } |
| 31 | + |
| 32 | +var peerId = new Buffer('01234567890123456789') |
| 33 | +var port = 6881 |
| 34 | + |
| 35 | +var client = new Client(peerId, port, parsedTorrent) |
| 36 | + |
| 37 | +// you must add an 'error' event handler! |
| 38 | +client.on('error', function (err) { |
| 39 | + console.log(err.message) |
| 40 | + // a tracker was unavailable or sent bad data to the client. you can probably ignore it |
| 41 | +}) |
| 42 | + |
| 43 | +client.start() // start getting peers from the tracker |
| 44 | + |
| 45 | +client.on('update', function (data) { |
| 46 | + console.log('got a response from tracker: ' + data.announce) |
| 47 | + console.log('number of seeders on this tracker: ' + data.complete) |
| 48 | + console.log('number of leechers on this tracker: ' + data.incomplete) |
| 49 | +}) |
| 50 | + |
| 51 | +client.once('peer', function (addr) { |
| 52 | + console.log('found a peer: ' + addr) // 85.10.239.191:48623 |
| 53 | +}) |
| 54 | + |
| 55 | +client.complete() // announce that download has completed (and you are now a seeder) |
| 56 | + |
| 57 | +client.update() // force a tracker announce. will trigger more 'update' events and maybe more 'peer' events |
| 58 | + |
| 59 | +client.stop() // stop getting peers from the tracker, gracefully leave the swarm |
| 60 | +``` |
| 61 | + |
| 62 | +**TODO:** Add BitTorrent tracker server implementation! |
| 63 | + |
| 64 | +```js |
| 65 | +var Server = require('bittorrent-tracker').Server |
| 66 | + |
| 67 | +``` |
| 68 | + |
| 69 | +## license |
| 70 | + |
| 71 | +MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). |
0 commit comments