2019-04-10 13:55

如何使用golang libp2p流将数据发送到多个主机?

  • stream
  • peer

my question refers to the libp2p library in golang:

This video explains the background:

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • drxzpo70788179614 drxzpo70788179614 2年前

    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.

    点赞 评论 复制链接分享