libp2p / go-libp2p-examples

Example libp2p applications
MIT License
339 stars 145 forks source link

chat doesn't broadcast to all peers #35

Open tanhuiya opened 5 years ago

tanhuiya commented 5 years ago

steps :

Then ,when I type any message such as

> aaa
> bbb

Only the second window receive aaa the third window receive bbb so, how can they receive all messages ?

anacrolix commented 5 years ago

I can confirm this behaviour. It doesn't appear that broadcasting to all connected peers is a documented behaviour. It could be if the receiver tracked all received streams, and duplicated messages amongst all peers.

JackBekket commented 5 years ago

Ok, look, first of all, you should know that Stream interface is a bidirectional link between two nodes, it's not a multichannel.

Also, you should know, that as far as example have only one stream variable instead of global mapping of streams - it will be owerwritten each time you have a new connection. If you want to communicate with multiple devices - you should have a stream mapping (or array) and append any new stream connection to it.

Then, if you want to implement multicast as your own, you should probably map each stream to some 'topic', and when you want to multicast message to every peer in topic list - you should send message to all streams on that topic.

However, instead of creating this mapping and creating multicast mechanism of your own, you can use PubSub mechanism, which has been created for those purposes.

You can find PubSub here: https://github.com/libp2p/go-libp2p-pubsub

P.S I would appreciate if someone will add PubSub example to this repo.