gostreampeerlibp2p

How to send data to multiple hosts using golang libp2p streams?


my question refers to the libp2p library in golang: https://github.com/libp2p/go-libp2p

This video explains the background: https://www.youtube.com/watch?v=hP0hSZ7E7_Y

The peers in the network communicate via buffered streams rw. For each new connecting peer there is a new stream created that connects him to an existing peer using addresses. This means there are multiple streams but not all peers are directly connected with each other. Using those streams, the peers can read and write data from it via

rw := bufio.NewReadWriter(bufio.NewReader(stream),bufio.NewWriter(stream))

by writing messages `rw.WriteString("message"),rw.Flush()``

and reading this messages via message := rw.ReadString(rw).

As the peers are not all connected, they only receive messages from directly connected peers and not even all of them if there are multiple connections.

Exactly this problem is mentioned in the video, around time 09:45. The author of the video says that this could be easily modified to send messages not only to directly connected peers but also to multiple hosts. But how can this be done?

My goal is to send message from one peer and that all other peers in the network, also the not directly connected ones, can receive (and answer) it.


Solution

  • The best way to send multicast or broadcast messages in libp2p is to use PubSub, So every node subscribed to a topic will be notified about sent messages despite not being directly connected to sender node.

    You can find an example implementation using Pubsub here. It uses gossibSub implementation from libp2p.