holepunchto / hyperswarm

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

Duplicate messages between connected servers #72

Closed draeder closed 3 years ago

draeder commented 3 years ago

I'm a little confused, and I apologize if it shows my ignorance/lack of experience.

I am trying to send a message from one connected node server to the other using:

  // you can now use the socket as a stream, eg:
    process.stdin.pipe(socket).pipe(process.stdout)

The problem is the message from one terminal is duplicated on the other.

For example, if I type the following in serverA:

test 123

I get the following in serverB:

test 123
test 123

. . . and vice versa

I can work around this by setting one of the two servers to not announce:

swarm.join(topic, {
  lookup: true, // find & connect to peers
  announce: false // <--------- don't announce, stops duplicates
})
draeder commented 3 years ago

I was able to solve this thanks to the code in example.js.

jamesfiltness commented 3 years ago

Could I ask which code specifically in example.js help you fix this issue?

draeder commented 3 years ago

@jamesfiltness

  if (client) process.stdin.pipe(socket) // client processes the message

or

  if (!client) process.stdin.pipe(socket) // server processes the message
james-filtness commented 3 years ago

thanks!