Welcome
Awesome Dat
Dat Applications
datproject/dat
datproject/dat-desktop
Community Applications
codeforscience/sciencefair
mafintosh/hyperirc
jondashkyle/soundcloud-archiver
mafintosh/hypervision
joehand/hypertweet
beakerbrowser/dat-photos-app
High-Level APIs
datproject/dat-node
datproject/dat-js
beakerbrowser/pauls-dat-api
beakerbrowser/node-dat-archive
Hosting & Dat Management
mafintosh/hypercore-archiver
datprotocol/hypercloud
beakerbrowser/hashbase
joehand/dat-now
mafintosh/hypercore-archiver-bot
joehand/hypercore-archiver-ws
datproject/dat-registry-api
datproject/dat-registry-client
Managing & Aggregating Dats
datproject/multidat
datproject/multidrive
jayrbolton/dat-pki
beakerbrowser/injestdb
Http Hosting
joehand/hyperdrive-http
beakerbrowser/dathttpd
Dat Link Utilties
datprotocol/dat-dns
joehand/dat-link-resolve
pfrazee/parse-dat-url
juliangruber/dat-encoding
Dat Utilities
joehand/dat-log
mafintosh/dat-ls
karissa/hyperhealth
joehand/hyperdrive-network-speed
File Imports & Exports
juliangruber/hyperdrive-import-files
mafintosh/mirror-folder
pfrazee/hyperdrive-staging-area
pfrazee/hyperdrive-to-zip-stream
Hypercore Tools
mafintosh/hyperpipe
Dat Core Modules
mafintosh/hyperdrive
mafintosh/hypercore
CLI Utilities
joehand/dat-doctor
joehand/dat-ignore
joehand/dat-json
Networking
karissa/hyperdiscovery
mafintosh/discovery-swarm
mafintosh/webrtc-swarm
joehand/dat-swarm-defaults
Lower level networking modules
maxogden/discovery-channel
mafintosh/dns-discovery
mafintosh/multicast-dns
webtorrent/bittorrent-dht
mafintosh/utp-native
mafintosh/signalhub
Storage
datproject/dat-storage
datproject/dat-secret-storage
Random Access
juliangruber/abstract-random-access
mafintosh/multi-random-access
mafintosh/random-access-file
mafintosh/random-access-memory
mafintosh/random-access-page-files
datproject/dat-http
substack/random-access-idb
Other Related Dat Project Modules
mafintosh/peer-network
mafintosh/hyperdht
Dat Project Organization Stuff
datproject/datproject.org
datproject/discussions
datproject/design
datproject/dat-elements
kriesse/dat-colors
kriesse/dat-icons
juliangruber/dat.json
Outdated
juliangruber/dat.haus
poga/hyperfeed
yoshuawuyts/normcore
yoshuawuyts/github-to-hypercore
poga/hyperspark
juliangruber/hypercore-index
juliangruber/hyperdrive-encoding
mafintosh/hyperdrive-http-server
joehand/hyperdrive-http
joehand/dat-push
joehand/dat-backup
joehand/archiver-server
joehand/archiver-api
poga/hyperdrive-ln
substack/hyperdrive-multiwriter
substack/hyperdrive-named-archives
substack/git-dat
CfABrigadePhiladelphia/jawn
maxogden/dat-archiver
juliangruber/hyperdrive-stats
karissa/hypercore-stats-server
mafintosh/hypercore-stats-ui
karissa/zip-to-hyperdrive
joehand/url-dat
joehand/tar-dat
joehand/hyperdrive-duplicate

hyperdht

A DHT that supports peer discovery and distributed hole punching

npm install hyperdht

Usage

First run a bootstrap node

npm install -g dht-rpc-bootstrap
dht-rpc-bootstrap --port=10000
var hyperdht = require('hyperdht')

var a = hyperdht({
  bootstrap: ['localhost:10000']
})

var b = hyperdht({
  bootstrap: ['localhost:10000']
})

a.ready(function () {
  // announce on a 32 byte key
  var key = new Buffer('01234567012345670123456701234567')

  b.announce(key, {port: 10000}, function (err) {
    if (err) throw err

    var stream = a.lookup(key)

    stream.on('data', function (data) {
      console.log('found peers:', data)
    })
  })
})

Usage

var dht = hyperdht([options])

Create a new dht. Options are passed to the dht-rpc constructor

var stream = dht.announce(key, [options], [callback])

Announce that you are listening on a key. Options include

{
  port: 10000, // port you are listening. If omitted the udp sockets port is used
  localAddress: {
    host: '192.168.1.2', // announce that you are listening on a local address also
    port: 8888
  }
}

The returned stream will emit peers as they are discovered during the announce face. The data events look like this

{
  node: {
    host: '10.4.2.4', // dht node's host
    port: 42424 // dht node's port
  },
  peers: [{
    host: '4.41.3.4', // a peer host
    port: 4244, // a peer port
  }],
  localPeers: [{
    host: '192.168.3.4', // a local peer host
    port: 4244, // a local peer port
  }]
}

Local peers will only contain addresses that share the first two parts of your local address (192.168 in the example). Both peers and localPeers will not contain your own address.

If you provide the callback the stream will be buffers and an array of results is passed. Note that you should keep announcing yourself at regular intervals (fx every 4-5min)

var stream = dht.lookup(key, [options], [callback])

Find peers but do not announce. Accepts the same options as announce and returns a similar stream.

dht.unannounce(key, [options], [callback])

Remove yourself from the DHT. Pass the same options as you used to announce yourself.

dht.ping(peer, callback)

Ping a another DHT peer. Useful if you want to see if you can connect to a node without holepunching.

dht.holepunch(peer, node, callback)

UDP holepunch to another peer. Pass the same node as was returned in the announce/lookup stream for the peer.

dht.ready(callback)

Wait for the dht to be fully bootstrapped. You do not need to call this before annnouncing / querying.

dht.destroy(callback)

Destroy the dht and stop listening.

dht.bootstrap([callback])

Re-bootstrap the DHT. Call this at regular intervals if you do not announce/lookup any keys.

dht.listen(port, callback)

Explicitly listen on a port. If you do not call this a random port will be chosen for you.

Command line tool

There is a command line tool available as well which is useful if you want to run a long lived dht node on a server or similar.

npm install -g hyperdht
hyperdht --help

To run a node simply pass it the addresses of your bootstrap servers

hyperdht --bootstrap=localhost:10000 --bootstrap=localhost:10001

License

MIT