I'm trying to use hyperdb in browser with swarming via webrtc and signalhub. The code is pretty strait forward, but there is some issue with hyperdb replicate where the connecting is killed because of a sameKey check in hypercore. So, I'm thinking ... I'm not properly juggling my discovery keys and id keys so the peers know they should be sync'd. Here is some sample code, it is a bit of a mess but the relevant bits are the hyperdb initialization and the webrtc/signalhub stuff (I think) ... the key at the top is the discovery key of the other peer:
const crypto = require('crypto'),
sha = crypto.createHash('sha1'),
hyperdb = require('hyperdb'),
hyperdiscovery = require('hyperdiscovery'),
cms = require('random-access-idb')('cms'),
webrtc = require('webrtc-swarm'),
signalhub = require('signalhub'),
hyperdrive = require('hyperdrive'),
pump = require('pump');
// Discovery key of other peer/signalhub channel
var key = "cbffda913dabfe73cbd45f64466ffda845383965e66b2aef5f3b716ee6c06528";
const db = hyperdb(filename => {
return cms(filename);
}, { valueEncoding: 'utf-8' });
var DEFAULT_SIGNALHUBS = 'https://signalhub-jccqtwhdwc.now.sh';
db.on('ready', function () {
const swarm = webrtc(signalhub(key, DEFAULT_SIGNALHUBS));
swarm.on('peer', function (conn) {
console.log("PEER!!!!!!!");
const peer = db.replicate({
upload: true,
download: true
});
pump(conn, peer, conn)
});
});