automerge / hypermerge

Build p2p collaborative applications without any server infrastructure in Node.js
MIT License
1.28k stars 66 forks source link

Problems with replicating between Repos #66

Closed johnptoohey closed 4 years ago

johnptoohey commented 4 years ago

Hi, Just found the project and it looked like a great synergy with automerge and hypercore. I'm struggling to get any code working. Projects tests (multi-repos.test.ts) fail with "extrac doc emitted and plan != count) errors. I cannot get the examples to work or even compile.

In my sample code below, my repoB never gets notified of changes made by repoA. Any help or pointers to working sample code would be greatly appreciated.

Thank you.

function getRepo(){
  const randomPath = uuid().toString()
  return new Repo({ path: randomPath, memory: true })
}

function getSwarm() {
  return Hyperswarm()
}

const swarm = getSwarm()
const repoA = getRepo()
const repoB = getRepo()

repoA.addSwarm(swarm,{announce: true})
repoB.addSwarm(swarm, {announce: true})

swarm.on('connection', (socket: any, details: any) => {
  console.log('new connection!', details)
})

const docUrl = repoA.create({ numbers: [ 2,3,4 ]})
repoB.watch<MyDoc>(docUrlB, state => {
  console.log("RepoB", state.numbers)
})

repoB.change<MyDoc>(docUrlB, state => {
    state.dominode = "rocks" 
    console.log("RepoB xx",state.dominode)
})

repoA.watch<MyDoc>(docUrl, state => {
  console.log("RepoA", state.numbers)
})

repoA.change<MyDoc>(docUrl, state => {
  state.numbers.push(5)
  state.foo = "bar"
  console.log("RepoA (Change)",state)
})
pvh commented 4 years ago

Hi John! It seems to work for me locally! Can you share a few extra details like how you're running the tests, node / yarn version, and OS?

johnptoohey commented 4 years ago

node: 13.3.0 ts-node: 8.5.4 OS: OSX 10.14 Tests: ts-node --files tests/* | tap-html --out jt.html Anything else that would be of use? Thanks

johnptoohey commented 4 years ago

Just noticed that in the test report, the Test (Three way docs don't load until all changes are in") seems to be calling the Watch callbacks that are set in test ("Share a doc between two repos"). The tests call repo.Close, and I would assume that kills the registered callbacks.

pvh commented 4 years ago

Would you try yarn test to isolate for environment issues?

johnptoohey commented 4 years ago

I don;t see a "test" script in the package.json, but running yarn tape give me the following summary 👎 1..269

tests 269

pass 225

fail 44

("tape": "ts-node --files node_modules/tape/bin/tape tests/*.test.ts")

johnptoohey commented 4 years ago

Did a clean clone of the project and used Yarn to install and run tests. Everything passes now. I'm coming from a Rust/Go background, so still coming up to speed with the Node toolchains. (I originally used npm to install and run tests etc.)

My own PoC was still not working, but then I noticed that I was sharing the same "swarm" variable between the Repos. Giving each one its own instance solved the problem.

Thanks again for all your help in getting this unblocked for me.

pvh commented 4 years ago

Ah, great to hear! I'm reading between the lines here but maybe this is a sign that NPM doesn't work right now?

johnptoohey commented 4 years ago

In my case, that seems to be true. NPM also takes longer to build the c libs and install. Yarn is much faster.

BTW Do I need to use swarm-discovery etc., to locate nodes on my network to replicate changes? I saw that in the example app, but much of that is outdated, so not sure if I should be using it.

pvh commented 4 years ago

The example app is embarrassingly bit-rotted! PushPin is the state of the art for an Electron app: https://github.com/inkandswitch/pushpin/ and for CLI: https://github.com/mjtognetti/pushpin-peer

I would be delighted to have a trivial example app to replace the ones there now so if you get a little demo going I'll gladly merge it.

johnptoohey commented 4 years ago

Thanks for these. I'll clean up my simple PoC at the weekend, and send you a PR for it. Simple two repos using file storage and showing data merged from RepoA to RepoB.

johnptoohey commented 4 years ago

Have a working sample ready to push. Can you add me to the project?

pvh commented 4 years ago

Great! Just fork the repo and send a PR, I'll give it a look-over and merge it as long as it works for me too.