Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peer-add event not firing for archives #41

Closed
KrishnaPG opened this issue Jan 27, 2020 · 5 comments
Closed

peer-add event not firing for archives #41

KrishnaPG opened this issue Jan 27, 2020 · 5 comments

Comments

@KrishnaPG
Copy link

Here is a reproducible code:


const SDK = require('dat-sdk')
const { Hypercore, Hyperdrive, resolveName, deleteStorage, destroy } = SDK();
  
const archive = Hyperdrive(null, {
  persist: false,
  createIfMissing: true,
});
  
archive.on('peer-add', (peer) => {
  console.log("[Owner]: peer added: ", peer); // <-- NOT GETTING INVOKED !!
});

archive.on('ready', () => {
  const url = `dat://${archive.key.toString('hex')}`;
  console.log(`Here's your URL: ${url}`, "discovery key: ", archive.discoveryKey.toString('hex'), " version: ", archive.version);
  setInterval(updateFile, 1000); // keep updating frequently	
  updateFile(() => runClient(archive)); // do an update once on start (before client connects, to avoid 404), then start client
});

function updateFile(cb) {
  const str = `world-${(new Date()).toLocaleTimeString()}`;
  archive.writeFile('/example.txt', str, () => {
    console.log('[Owner]: Written example file!', str);
    if(cb) cb();
  });
}

/*-------------------------*/
/******* client ************/
/*-------------------------*/	
function runClient(archive) {
  var remoteArchive = Hyperdrive(archive.key.toString('hex'), {
    persist: false,
    createIfMissing: false
  });
  remoteArchive.on('peer-add', (peer) => {
    console.log("  [client] peer added: ", peer);
  });
  reallyReady(remoteArchive, () => {
    remoteArchive.readdir('/', console.log)
  })
  function reallyReady(archive, cb) {
    if (archive.metadata.peers.length) {
      archive.metadata.update({ ifAvailable: true }, cb)
    } else {
      archive.metadata.once('peer-add', () => { 
        archive.metadata.update({ ifAvailable: true }, cb)
      })
    }
  }
  remoteArchive.readFile('/example.txt', 'utf8', (err, data) => {
    if (err) throw err
    console.log(`  [client]: content of example.txt: ${data}`);

    remoteArchive.on('update', () => {
      console.log("  [client]:  remoteArchive updated: ", archive.version);
    });
  })	
}

What this does:

  • creates a new archive and keeps updating a file inside it. Also creates a test client
  • the client is able to receive update notifications from the owner for the file whenever it changes

What is not working:

  • the peer-add event is not getting fired on owner or client

Sample output:

Here's your URL: dat://72e0c3d4c61778020876a8ced49cfa683c63cc3a235c1b7d6aae75d8492fd504 discovery key:  05b9e8f264f076260c22a664adeaf2663b2c43c39800877cb36006d48e6f8dd7  version:  0
[Owner]: Written example file! world-7:36:20 AM
  [client]: content of example.txt: world-7:36:20 AM
  [client]:  remoteArchive updated:  2
[Owner]: Written example file! world-7:36:21 AM
  [client]:  remoteArchive updated:  3
[Owner]: Written example file! world-7:36:22 AM
  [client]:  remoteArchive updated:  4
[Owner]: Written example file! world-7:36:23 AM
  [client]:  remoteArchive updated:  5
[Owner]: Written example file! world-7:36:24 AM
  [client]:  remoteArchive updated:  6
[Owner]: Written example file! world-7:36:25 AM
  [client]:  remoteArchive updated:  7
[Owner]: Written example file! world-7:36:26 AM
  [client]:  remoteArchive updated:  8
[Owner]: Written example file! world-7:36:27 AM
  [client]:  remoteArchive updated:  9
@KrishnaPG
Copy link
Author

KrishnaPG commented Jan 27, 2020

After debugging, it looks like the peer-add events were added in version 10+ in the hyperDrive, whereas dat-sdk is referring to version 9+. This seems to be the reason why the peer-add events were not getting generated on the archive.

Reference: holepunchto/hyperdrive@4bbb240

@RangerMauve
Copy link
Owner

Dang, I'll try to do a patch to the v9 branch of hyperdrive to get it in.

@okdistribute
Copy link
Contributor

thanks @RangerMauve and @KrishnaPG for reporting!

I'd suggest doing little to edit the old version since we're trying to move to the next one and instead focusing on moving the sdk to v10, given limited resources. but backwards compatibility is cool too :) although editing v9 might not be as worth it in the coming months/years

@RangerMauve
Copy link
Owner

I think that given it's a small PR with no other changes in the works, it'd be worth it to get in. 😁

@RangerMauve
Copy link
Owner

Out in dat-sdk @1.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants