# Notes on libp2p examples ## How to put modules together? - Compare different files to summarize the right way to use it. - Initiate a libp2p node - Config a protocol - Set stream handler ## Examples ### HTTP Proxy - Local peer listens to http request and coverts redirect that http request via the `example protocol` - function serverHTTP fits HTTP reqs to example protocol - Proxy peer handles the example protocol, extracts the HTTP req, sends the req, fits the response back to the example protocol stream ### Echo - `makeHost` ### Multipro - How to design a protocol interface? - Wrap the protocol with a struct - Define constructor, handler and action - How to integrate the protocol with protobuf? - Define message types in .proto file - Protobuf will auto-generate a file that has a module that is used as a data container - How to integrate the host with the protcol to construct a service node? - Wrap the node with a struct (Composition pattern) - The `Node` struct includes a `host` and protocols and also defines some additional operations such as signing and verifying digital signatures. ### Chat - How many wrappers are there before the meesage can be sent? - `bufio.NewReader(stream)` - `ptorobufio.NewFullWriter(stream)` - `stream.write(data)` - Keep stream open to send and receive data - What happens if the stream is closed? ### Chat-with-rendezvous - How does the rendezvous point work? - Use discovery interface - DHT routing module implements the discovery interface - Why `Provide` and `FindProvider`? This is the rendezvous scheme - Not very efficitent for nodes that are in the same local network - How to connect to multiple nodes? ### Chat-with-mdns - Why are there 2 `discovery` interface? - How does mDNS work? - Multicast DNS (MDNS) works by sending DNS packets over UDP to a certain multicast address. All mdns-capable hosts in the network also listen to this address. - Service discovery is a two step process. - The first step is finding the names of all hosts providing a certain service (e.g. printing). This will not yet give you the ip address, instead it gives you the mdns name (ending with `.local`). - The second step in service discovery is to resolve the `.local` name of the host over mdns. You ask via multicast who `foo.local` is, `foo.local` will see that packet, and respond via broadcast with its ip address, port number and other information. - In this example, the service name is the rendezvous string. The lookup begins as long as the mDNS service is up. - The mechanism of Rendenzvous points is very similar to mDNS. ### IPFS Camp - How does gossipsub work? ### Taipei 2019 - How does gossipsub work? - Subscribe a new topic - Use a generator `subscripton.Next()` to pull the received messages from the subscribed topic - Publishe a message to that topic ## What are the components? Do they all have example covered? - Protobuf - Peer discovery/lookup - Rendezvous point - mDNS - DHT routing module - DHT - Routing module - Multiplexing/Swarm/Stream - NAT/UPnP/Hole punching? - bufio? - SOCKS/Proxy? - Peerstore? ## Reference - [PR of libp2p-example](https://github.com/libp2p/go-libp2p-examples/pull/85) - [How exactly does mDNS resolve addresses?](https://stackoverflow.com/questions/11835782/how-exactly-does-mdns-resolve-addresses) ###### tags: `note`