// remove request from map as we have processed it here
delete(p.requests,data.MessageData.Id)
}else{
log.Println("Failed to locate request data boject for response")
return
}
log.Printf("%s: Received ping response from %s. Message id:%s. Message: %s.",s.Conn().LocalPeer(),s.Conn().RemotePeer(),data.MessageData.Id,data.Message)
p.done<-true
}
func(p*PingProtocol)Ping(hosthost.Host)bool{
log.Printf("%s: Sending ping to: %s....",p.node.ID(),host.ID())
# Protocol Multiplexing using multicodecs with libp2p
This examples shows how to use multicodecs (i.e. json) to encode and transmit information between LibP2P hosts using LibP2P Streams.
Multicodecs present a common interface, making it very easy to swap the codec implementation if needed.
This example expects that you area already familiar with the [echo example](https://github.com/libp2p/go-libp2p/tree/master/examples/echo).
## Build
From `go-libp2p` base folder:
```
> make deps-protocol-muxing
> go build -o multicodecs ./examples/protocol-multiplexing-with-multicodecs
```
## Usage
```
> ./multicodecs
```
## Details
The example creates two LibP2P Hosts. Host1 opens a stream to Host2. Host2 has an `StreamHandler` to deal with the incoming stream. This is covered in the `echo` example.
Both hosts simulate a conversation. But rather than sending raw messages on the stream, each message in the conversation is encoded under a `json` object (using the `json` multicodec). For example:
```
{
"Msg": "This is the message",
"Index": 3,
"HangUp": false
}
```
The stream lasts until one of the sides closes it when the HangUp field is `true`.