forked from libp2p/js-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.js
38 lines (33 loc) · 1.02 KB
/
1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* eslint-disable no-console */
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { Mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'
import { Bootstrap } from '@libp2p/bootstrap'
import bootstrapers from './bootstrappers.js'
;(async () => {
const node = await createLibp2p({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [new TCP()],
streamMuxers: [new Mplex()],
connectionEncryption: [new Noise()],
peerDiscovery: [
new Bootstrap({
interval: 60e3,
list: bootstrapers
})
]
})
node.connectionManager.addEventListener('peer:connect', (evt) => {
const connection = evt.detail
console.log('Connection established to:', connection.remotePeer.toString()) // Emitted when a peer has been found
})
node.addEventListener('peer:discovery', (evt) => {
const peer = evt.detail
// No need to dial, autoDial is on
console.log('Discovered:', peer.id.toString())
})
await node.start()
})();