Skip to content

Commit f04e182

Browse files
2colormaschad
andauthored
docs: update mdns example and docs (#3056)
* docs: update mdns example and docs * docs: add link to specs --------- Co-authored-by: Daniel N <[email protected]> Co-authored-by: Chad Nehemiah <[email protected]>
1 parent d53ef17 commit f04e182

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

doc/CONFIGURATION.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,13 @@ If you want to know more about libp2p connection encryption, you should read the
127127
128128
Some available peer discovery modules are:
129129

130-
- [@libp2p/mdns](https://github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-mdns)
131-
- [@libp2p/bootstrap](https://github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-bootstrap)
132-
- [@libp2p/kad-dht](https://github.com/libp2p/js-libp2p/tree/main/packages/kad-dht)
133-
- [@chainsafe/discv5](https://github.com/chainsafe/discv5)
130+
- [@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))
131+
- [@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))
132+
- [@libp2p/bootstrap](https://github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-bootstrap) (typically used together with @libp2p/kad-dht)
133+
- [@chainsafe/discv5](https://github.com/chainsafe/discv5) ([spec](https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md))
134134

135135
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).
136136

137-
If you want to know more about libp2p peer discovery, you should read the following content:
138-
139-
- https://github.com/libp2p/specs/blob/master/discovery/mdns.md
140-
141137
### Content Routing
142138

143139
> 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.
@@ -268,7 +264,7 @@ const node = await createLibp2p({
268264
],
269265
streamMuxers: [yamux()],
270266
connectionEncrypters: [noise()],
271-
peerDiscovery: [MulticastDNS],
267+
peerDiscovery: [mdns()],
272268
services: {
273269
dht: kadDHT(),
274270
pubsub: gossipsub()
@@ -303,8 +299,17 @@ const node = await createLibp2p({
303299
})
304300
]
305301
})
302+
303+
node.addEventListener('peer:discovery', (event) => {
304+
console.log('Discovered new peer:', event.detail.id.toString())
305+
node.dial(event.detail.multiaddrs)
306+
})
306307
```
307308

309+
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.
310+
311+
312+
308313
#### Customizing Pubsub
309314

310315
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.

packages/peer-discovery-mdns/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const libp2p = await createLibp2p({
3939
})
4040

4141
libp2p.addEventListener('peer:discovery', (evt) => {
42+
libp2p.dial(evt.detail.multiaddrs) // dial discovered peers
4243
console.log('found peer: ', evt.detail.toString())
4344
})
4445
```

packages/peer-discovery-mdns/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* })
1717
*
1818
* libp2p.addEventListener('peer:discovery', (evt) => {
19+
* libp2p.dial(evt.detail.multiaddrs) // dial discovered peers
1920
* console.log('found peer: ', evt.detail.toString())
2021
* })
2122
* ```

0 commit comments

Comments
 (0)