Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
7 contributors

Users who have contributed to this file

@alanshaw @vasco-santos @vutsalsinghal @PascalPrecht @olizilla @daviddias @achingbrain
146 lines (85 sloc) 3.97 KB

DHT API

dht.findPeer

Retrieve the Peer Info of a reachable node in the network.

ipfs.dht.findPeer(peerId, [callback])

Where peerId is a IPFS/libp2p Id from PeerId type.

callback must follow function (err, peerInfo) {} signature, where err is an error if the operation was not successful. peerInfo is an object of type PeerInfo.

If no callback is passed, a promise is returned.

Example:

var id = PeerId.create()

ipfs.dht.findPeer(id, function (err, peerInfo) {
  // peerInfo will contain the multiaddrs of that peer
  const id = peerInfo.id
  const addrs = peerInfo.multiaddrs
})

A great source of examples can be found in the tests for this API.

dht.findProvs

Retrieve the providers for content that is addressed by an hash.

ipfs.dht.findProvs(hash, [options], [callback])

Where hash is a multihash.

options an optional object with the following properties

  • timeout - a maximum timeout in milliseconds
  • maxNumProviders - a maximum number of providers to find

callback must follow function (err, peerInfos) {} signature, where err is an error if the operation was not successful. peerInfos is an array of type [PeerInfo]. Each entry of this array is composed by the peerId, as well as an array with its adresses.

If no callback is passed, a promise is returned.

Example:

ipfs.dht.findProvs(multihash, function (err, res) {})

ipfs.dht.findProvs(multihash, { timeout: 4000 }, function (err, res) {})

A great source of examples can be found in the tests for this API.

dht.get

Retrieve a value from DHT

ipfs.dht.get(key, [callback])

Where key is a Buffer.

callback must follow function (err, value) {} signature, where err is an error if the operation was not successful. value is the value that was stored under that key.

If no callback is passed, a promise is returned.

Example:

ipfs.dht.get(key, function (err, value) {})

A great source of examples can be found in the tests for this API.

dht.provide

Announce to the network that you are providing given values.

ipfs.dht.provide(cid, [callback])

Where cid is a CID or array of CIDs.

callback must follow function (err) {} signature, where err is an error if the operation was not successful.

If no callback is passed, a promise is returned.

Example:

ipfs.dht.provide(cid, function (err) {})

A great source of examples can be found in the tests for this API.

dht.put

Store a value on the DHT

ipfs.dht.put(key, value, [callback])

Where key is a Buffer and value is a Buffer.

callback must follow function (err) {} signature, where err is an error if the operation was not successful.

If no callback is passed, a promise is returned.

Example:

ipfs.dht.put(key, value, function (err) {})

A great source of examples can be found in the tests for this API.

dht.query

Queries the network for the 'closest peers' to a given key. 'closest' is defined by the rules of the underlying Peer Routing mechanism.

ipfs.dht.query(peerId, [callback])

Where peerId is a IPFS/libp2p Id of type PeerId.

callback must follow function (err, peerInfos) {} signature, where err is an error if the operation was not successful. peerInfos is an array of objects of type PeerInfo

If no callback is passed, a promise is returned.

Example:

const id = PeerId.create()

ipfs.dht.query(id, function (err, peerInfos) {
})

A great source of examples can be found in the tests for this API.

You can’t perform that action at this time.