Created
March 12, 2023 13:23
-
-
Save moskalyk/b9c23d01bc7bc0c6b132e44a741674ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dht server, run with $ node dht.js | |
const DHT = require('@hyperswarm/dht') | |
const dhtNode = new DHT() | |
const Corestore = require("corestore") | |
const server = dhtNode.createServer() | |
const corestore = new Corestore('./db') | |
const data = corestore.get({name: "data", valueEncoding: 'json'}) | |
server.on('connection', function (socket) { | |
data.replicate(socket) | |
console.log('Remote public key', socket.remotePublicKey) | |
}) | |
const keyPair = DHT.keyPair(); | |
(async () => { | |
await server.listen(keyPair) | |
setInterval(async () => { | |
await data.append({data: Date.now()}) | |
}, 2000) | |
})(); | |
data.createReadStream({live: true}).on('data', (data) => { | |
console.log(data) | |
}) | |
console.log(keyPair.publicKey.toString('hex')) | |
// client run with $ node client_dht.js <public_key> | |
const DHT = require('@hyperswarm/dht') | |
const Corestore = require("corestore") | |
const ram = require('random-access-memory') | |
const node = new DHT(); | |
(() => { | |
const corestore = new Corestore(ram, {valueEncoding: 'json'}) | |
const data = corestore.get({ | |
key: Buffer.from(process.argv[2], 'hex'), | |
valueEncoding: "utf-8" | |
}) | |
const publicKey = process.argv[2] | |
const socket = node.connect(Buffer.from(publicKey, 'hex')) | |
socket.on('open', function () { | |
data.replicate(socket) | |
}) | |
data.createReadStream({live: true}).on('data', (data) => { | |
console.log(data) | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment