holepunchto / hyperswarm

A distributed networking stack for connecting peers.
https://docs.holepunch.to
MIT License
1.04k stars 84 forks source link

No peer connections if joining after swarm initialization #67

Open freddi301 opened 3 years ago

freddi301 commented 3 years ago

testing on the same machine, and 2 machines over LAN

two nodes connect as expected

// pc a
const swarm = hyperswarm()
swarm.join(hash, {lookup:true, announce: true})
swarm.on("connection", () => { console.log("yuppie") })
// pc b
const swarm = hyperswarm()
swarm.join(hash, {lookup:true, announce: true})
swarm.on("connection", () => { console.log("yuppie") })

two nodes does not connect

// pc a
const swarm = hyperswarm()
setTimeout(() => swarm.join(hash, {lookup:true, announce: true}), 5000)
swarm.on("connection", () => { console.log("never happens") })
// pc b
const swarm = hyperswarm()
setTimeout(() => swarm.join(hash, {lookup:true, announce: true}), 10000)
swarm.on("connection", () => { console.log("never happens") })

workaround (the nodes connect)

// pc a
const swarm = hyperswarm()
swarm.network.bind()
setTimeout(() => swarm.join(hash, {lookup:true, announce: true}), 5000)
swarm.on("connection", () => { console.log("yuppie") })
// pc b
const swarm = hyperswarm()
swarm.network.bind()
setTimeout(() => swarm.join(hash, {lookup:true, announce: true}), 10000)
swarm.on("connection", () => { console.log("yuppie") })

Maybe i am missing something in the docs? Maybe it has something to do with how discovery cycle works?

mafintosh commented 3 years ago

When you say “working” what do you mean exactly? Can you provide a full gist of a non working example? That’ll make it easier to help

freddi301 commented 3 years ago

repo https://github.com/freddi301/p2p-network-tools/tree/master/desktop If i join a common topic right after initializing the swarm, the two nodes do connect. If i join a common topic some time after initializing swarm the two nodes do not connect. If i call swarm.network.bind() right after swarm initialization and join a common topic some time after, the two nodes do connect. I updated the description