mafintosh / discovery-swarm

A network swarm that uses discovery-channel to find peers
MIT License
376 stars 57 forks source link

How to connect to another peer only once? #61

Closed tetsuo closed 5 years ago

tetsuo commented 5 years ago

I want to connect to another peer only once regardless of the connection type or if I'm the initiator or not.

What I'm expecting is to be able to just destroy a socket if it's not needed. But, once I do, it tries to re-connect.

What is the proper way of making sure that you're connected to a peer already and there are no dangling sockets?

let swarm = require('discovery-swarm')

let swx = swarm()
let swy = swarm()

swx.listen(0)
swy.listen(0)

swx.join('bla-ubuntu-14.04')
swy.join('bla-ubuntu-14.04')

let swx_connected = false

swx.on('connection', (conn, details) => {
  console.log('found + connected to peer 1', details.initiator)
  if (swx_connected) {
    console.log('swx already connected, disconnecting')
    conn.destroy()
    return
  }
  swx_connected = true
})

let swy_connected = false

swy.on('connection', (conn, details) => {
  console.log('found + connected to peer 2', details.initiator)
  if (swy_connected) {
    console.log('swy already connected, disconnecting')
    conn.destroy()
    return
  }
  swy_connected = true
})
AbleLincoln commented 3 years ago

Hi @tetsuo how did you solve this?

tetsuo commented 3 years ago

Hey @AbleLincoln, I actually couldn't. I forgot what the issue was, but I guess it was related to not being able to disable one of the discovery methods that is running in the background.

You can try hyperswarm, looks like that one is actively maintained.

AbleLincoln commented 3 years ago

Thank you @tetsuo, I checked out that library and it works perfectly for my needs.