title: How to handle buffered Read-Write Stream(s) to peers in golang using libp2p?
body: <p>I am following this tutorial:</p>
  
  <p><a href="https://github.com/libp2p/go-libp2p-examples/tree/master/chat-with-mdns" rel="nofollow noreferrer">https://github.com/libp2p/go-libp2p-examples/tree/master/chat-with-mdns</a></p>
  
  <p>In a short form, it:</p>
  
  <ol>
  <li>configures a p2p host</li>
  <li>sets a default handler function for incoming connections
  (3. not necessary) </li>
  <li>and opens a stream to the connecting peers:</li>
  </ol>
  
  <p><code>stream, err := host.NewStream(ctx, peer.ID, protocol.ID(cfg.ProtocolID))</code></p>
  
  <p>Afterwards, there is a buffer stream/read-write variable created:</p>
  
  <p><code>rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))</code></p>
  
  <p>Now this stream is used to send and receive data between the peers. This is done using two goroutine functions that have rw as an input: </p>
  
  <p><code>go writeData(rw)</code>
  <code>go readData(rw)</code></p>
  
  <p>My problems are:</p>
  
  <ol>
  <li><p>I want to send data to my peers and need feedback from them:
  e.g. in rw there is a question and they need to answer yes/no. How can I transfer back this answer and process it (enable some interaction)?</p></li>
  <li><p>The data I want to send in rw is not always the same. Sometimes it's a string containing only a name, sometimes it's a string containing a whole block etc. How can I distinguish? </p></li>
  </ol>
  
  <p>I thought about those solutions. But I am new to golang, so maybe you have a better one:</p>
  
  <ul>
  <li><p>do I need a new stream for every different content:
  <code>stream, err := host.NewStream(ctx, peer.ID, protocol.ID(cfg.ProtocolID))</code></p></li>
  <li><p>do I need to open more buffered rw varibales for every different content:
  <code>rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream))</code></p></li>
  <li><p>are there any other solutions?</p></li>
  </ul>
  
  <p>Thank you for any help to solve this!!</p>