Skip to content

Commit 4e36360

Browse files
authored
feat: add discovery mechansims example (#92)
Restores example focussed on discovery mechanisms.
1 parent 21d758b commit 4e36360

File tree

16 files changed

+482
-0
lines changed

16 files changed

+482
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
- js-libp2p-example-circuit-relay
2626
- js-libp2p-example-connection-encryption
2727
- js-libp2p-example-delegated-routing
28+
- js-libp2p-example-discovery-mechanisms
2829
defaults:
2930
run:
3031
working-directory: examples/${{ matrix.project }}
@@ -77,6 +78,7 @@ jobs:
7778
- js-libp2p-example-circuit-relay
7879
- js-libp2p-example-connection-encryption
7980
- js-libp2p-example-delegated-routing
81+
- js-libp2p-example-discovery-mechanisms
8082
steps:
8183
- uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be
8284
with:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Feel free to jump directly into the examples, however going through the followin
5151
- [Circuit Relay](./examples/js-libp2p-example-circuit-relay) - configuring Circuit Relay connections
5252
- [Connection Encryption](./examples/js-libp2p-example-connection-encryption) - how to encrypt connection between libp2p nodes
5353
- [Delegated routing](./examples/js-libp2p-example-delegated-routing) - how to offload network operations and queries onto more capable nodes
54+
- [Discovery mechanisms](./examples/js-libp2p-example-discovery-mechanisms) - how libp2p discovers other peers on the network
5455

5556
#### Other examples
5657

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ⚠️ IMPORTANT ⚠️
2+
3+
# Please do not create a Pull Request for this repository
4+
5+
The contents of this repository are automatically synced from the parent [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples) so any changes made to the standalone repository will be lost after the next sync.
6+
7+
Please open a PR against [js-libp2p Examples](https://github.com/libp2p/js-libp2p-examples) instead.
8+
9+
## Contributing
10+
11+
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.
12+
13+
1. Fork the [js-libp2p Examples Project](https://github.com/libp2p/js-libp2p-examples)
14+
2. Create your Feature Branch (`git checkout -b feature/amazing-example`)
15+
3. Commit your Changes (`git commit -a -m 'feat: add some amazing example'`)
16+
4. Push to the Branch (`git push origin feature/amazing-example`)
17+
5. Open a Pull Request
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: pull
2+
3+
on:
4+
workflow_dispatch
5+
6+
jobs:
7+
sync:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Pull from another repository
12+
uses: ipfs-examples/actions-pull-directory-from-repo@main
13+
with:
14+
source-repo: libp2p/js-libp2p-examples
15+
source-folder-path: examples/${{ github.event.repository.name }}
16+
source-branch: main
17+
target-branch: main
18+
git-username: github-actions
19+
git-email: [email protected]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-disable no-console */
2+
3+
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { bootstrap } from '@libp2p/bootstrap'
6+
import { identify } from '@libp2p/identify'
7+
import { kadDHT } from '@libp2p/kad-dht'
8+
import { mplex } from '@libp2p/mplex'
9+
import { tcp } from '@libp2p/tcp'
10+
import { createLibp2p } from 'libp2p'
11+
import bootstrappers from './bootstrappers.js'
12+
13+
const node = await createLibp2p({
14+
addresses: {
15+
listen: ['/ip4/0.0.0.0/tcp/0']
16+
},
17+
transports: [tcp()],
18+
streamMuxers: [yamux(), mplex()],
19+
connectionEncryption: [noise()],
20+
peerDiscovery: [
21+
bootstrap({
22+
list: bootstrappers
23+
})
24+
],
25+
services: {
26+
kadDHT: kadDHT(),
27+
identify: identify()
28+
}
29+
})
30+
31+
node.addEventListener('peer:connect', (evt) => {
32+
const peerId = evt.detail
33+
console.log('Connection established to:', peerId.toString()) // Emitted when a peer has been found
34+
})
35+
36+
node.addEventListener('peer:discovery', (evt) => {
37+
const peerInfo = evt.detail
38+
39+
console.log('Discovered:', peerInfo.id.toString())
40+
})
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable no-console */
2+
3+
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { mdns } from '@libp2p/mdns'
6+
import { mplex } from '@libp2p/mplex'
7+
import { tcp } from '@libp2p/tcp'
8+
import { createLibp2p } from 'libp2p'
9+
10+
const createNode = async () => {
11+
const node = await createLibp2p({
12+
addresses: {
13+
listen: ['/ip4/0.0.0.0/tcp/0']
14+
},
15+
transports: [
16+
tcp()
17+
],
18+
streamMuxers: [
19+
yamux(), mplex()
20+
],
21+
connectionEncryption: [
22+
noise()
23+
],
24+
peerDiscovery: [
25+
mdns({
26+
interval: 20e3
27+
})
28+
]
29+
})
30+
31+
return node
32+
}
33+
34+
;(async () => {
35+
const [node1, node2] = await Promise.all([
36+
createNode(),
37+
createNode()
38+
])
39+
40+
node1.addEventListener('peer:discovery', (evt) => console.log('Discovered:', evt.detail.id.toString()))
41+
node2.addEventListener('peer:discovery', (evt) => console.log('Discovered:', evt.detail.id.toString()))
42+
})()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* eslint-disable no-console */
2+
3+
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { bootstrap } from '@libp2p/bootstrap'
6+
import { floodsub } from '@libp2p/floodsub'
7+
import { identify } from '@libp2p/identify'
8+
import { mplex } from '@libp2p/mplex'
9+
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
10+
import { tcp } from '@libp2p/tcp'
11+
import { createLibp2p } from 'libp2p'
12+
13+
const createNode = async (bootstrappers = []) => {
14+
const config = {
15+
addresses: {
16+
listen: ['/ip4/0.0.0.0/tcp/0']
17+
},
18+
transports: [tcp()],
19+
streamMuxers: [yamux(), mplex()],
20+
connectionEncryption: [noise()],
21+
peerDiscovery: [
22+
pubsubPeerDiscovery({
23+
interval: 1000
24+
})
25+
],
26+
services: {
27+
pubsub: floodsub(),
28+
identify: identify()
29+
}
30+
}
31+
32+
if (bootstrappers.length > 0) {
33+
config.peerDiscovery.push(bootstrap({
34+
list: bootstrappers
35+
}))
36+
}
37+
38+
return await createLibp2p(config)
39+
}
40+
41+
const bootstrapper = await createNode([])
42+
43+
console.log(`libp2p bootstrapper started with id: ${bootstrapper.peerId.toString()}`)
44+
45+
const bootstrapperMultiaddrs = bootstrapper.getMultiaddrs().map((m) => m.toString())
46+
47+
const [node1, node2] = await Promise.all([
48+
createNode(bootstrapperMultiaddrs),
49+
createNode(bootstrapperMultiaddrs)
50+
])
51+
52+
node1.addEventListener('peer:discovery', (evt) => {
53+
const peer = evt.detail
54+
console.log(`Peer ${node1.peerId.toString()} discovered: ${peer.id.toString()}`)
55+
})
56+
node2.addEventListener('peer:discovery', (evt) => {
57+
const peer = evt.detail
58+
console.log(`Peer ${node2.peerId.toString()} discovered: ${peer.id.toString()}`)
59+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
 (0)