Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update mdns example and docs #3056

Merged
merged 3 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,13 @@ If you want to know more about libp2p connection encryption, you should read the

Some available peer discovery modules are:

- [@libp2p/mdns](https://github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-mdns)
- [@libp2p/bootstrap](https://github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-bootstrap)
- [@libp2p/kad-dht](https://github.com/libp2p/js-libp2p/tree/main/packages/kad-dht)
- [@chainsafe/discv5](https://github.com/chainsafe/discv5)
- [@libp2p/mdns](https://github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-mdns) ([spec](https://github.com/libp2p/specs/blob/master/discovery/mdns.md))
- [@libp2p/kad-dht](https://github.com/libp2p/js-libp2p/tree/main/packages/kad-dht) ([spec](https://github.com/libp2p/specs/blob/master/kad-dht/README.md))
- [@libp2p/bootstrap](https://github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-bootstrap) (typically used together with @libp2p/kad-dht)
- [@chainsafe/discv5](https://github.com/chainsafe/discv5) ([spec](https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md))

If none of the available peer discovery protocols fulfills your needs, you can create a libp2p compatible one. A libp2p peer discovery protocol just needs to be compliant with the [Peer Discovery Interface](https://github.com/libp2p/js-libp2p/tree/main/packages/interface/src/peer-discovery).

If you want to know more about libp2p peer discovery, you should read the following content:

- https://github.com/libp2p/specs/blob/master/discovery/mdns.md

### Content Routing

> Content routing provides a way to find where content lives in the network. It works in two steps: 1) Peers provide (announce) to the network that they are holders of specific content and 2) Peers issue queries to find where that content lives. A Content Routing mechanism could be as complex as a DHT or as simple as a registry somewhere in the network.
Expand Down Expand Up @@ -268,7 +264,7 @@ const node = await createLibp2p({
],
streamMuxers: [yamux()],
connectionEncrypters: [noise()],
peerDiscovery: [MulticastDNS],
peerDiscovery: [mdns()],
services: {
dht: kadDHT(),
pubsub: gossipsub()
Expand Down Expand Up @@ -303,8 +299,17 @@ const node = await createLibp2p({
})
]
})

node.addEventListener('peer:discovery', (event) => {
console.log('Discovered new peer:', event.detail.id.toString())
node.dial(event.detail.multiaddrs)
})
```

Note the `bootstrap` peer discovery module will automatically dial the bootstrap peers when the node starts up, while `mdns` will only trigger the `peer:discovery` event when a new peer is discovered.



#### Customizing Pubsub

Before a peer can subscribe to a topic it must find other peers and establish network connections with them. The pub/sub system doesn’t have any way to discover peers by itself. Instead, it relies upon the application to find new peers on its behalf, a process called ambient peer discovery.
Expand Down
1 change: 1 addition & 0 deletions packages/peer-discovery-mdns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const libp2p = await createLibp2p({
})

libp2p.addEventListener('peer:discovery', (evt) => {
libp2p.dial(evt.detail.multiaddrs) // dial discovered peers
console.log('found peer: ', evt.detail.toString())
})
```
Expand Down
1 change: 1 addition & 0 deletions packages/peer-discovery-mdns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* })
*
* libp2p.addEventListener('peer:discovery', (evt) => {
* libp2p.dial(evt.detail.multiaddrs) // dial discovered peers
* console.log('found peer: ', evt.detail.toString())
* })
* ```
Expand Down