When is it safe to send extension messages to a new peer
RangerMauve opened this issue · 1 comments
Hey, I think there's a bit of a race condition in a lot of the extension code I've been working with.
The code looks something like this on both sides:
const core = hypercore(somekey)
const ext = core.registerExtension('name', {
onmessage: doSomething
})
core.on('peer-add', (peer) => {
ext.send('some initial handshake', peer)
})
What ends up happening is that the messages on peer-add don't end up being sent.
I think this is due to the options message with the extension name hasn't been processed on both sides by the time peer-add
is emitted. Adding a setTimeout of a second seems to address the issue, but it'd obviously be better to wait for some other event if possible.
Is there some way I could listen on the options message having gone through or some way to listen on the peers extensions list changing? Or is this maybe something that can be handled within the registerExtension logic?
Checked the code and it looks like it's safe to send extensions after the peer-open
event since the options message gets sent just before that's emitted.
So this should work:
const core = hypercore(somekey)
const ext = core.registerExtension('name', {
onmessage: doSomething
})
core.on('peer-open', (peer) => {
ext.send('some initial handshake', peer)
})