dht-rpc
Make RPC calls over a Kademlia based DHT.
npm install dht-rpc
Usage
Here is an example implementing a simple key value store
First spin up a bootstrap node. You can make multiple if you want for redundancy.
var dht = require('dht-rpc')
var bootstrap = dht({ephemeral: true})
bootstrap.listen(10001)
Now lets make some dht nodes that can store values in our key value store.
var dht = require('dht-rpc')
var crypto = require('crypto')
for (var i = 0; i < 100; i++) createNode()
function createNode () {
var node = dht({
bootstrap: ['localhost:10001']
})
var values = {}
node.on('closest:store', function (query, cb) {
if (!query.value) return cb()
var key = sha256(query.value).toString('hex')
values[key] = query.value
console.log('Storing', key, '-->', query.value.toString())
cb()
})
node.on('query:lookup', function (query, cb) {
var value = values[query.target.toString('hex')]
cb(null, value)
})
}
function sha256 (val) {
return crypto.createHash('sha256').update(val).digest('hex')
}
To insert a value into this dht make another script that does this following
var node = dht({ephemeral: true})
node.closest({command: 'store', target: sha256(val), value: val}, function (err, res) {
if (err) throw err
console.log('Inserted', sha256(val).toString('hex'))
})
Then after inserting run this script to query for a value
node.query({command: 'lookup', target: new Buffer(hexFromAbove, 'hex')})
.on('data', function (data) {
if (data.value && sha256(data.value).toString('hex') === hexFromAbove) {
console.log(val, '-->', data.value.toString())
this.destroy()
}
})
.on('end', function () {
console.log('(query finished)')
})
License
MIT