generated from libp2p/js-libp2p-example-fork-go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
171 lines (144 loc) · 5.33 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
import { dcutr } from '@libp2p/dcutr'
import { identify } from '@libp2p/identify'
import { webRTC } from '@libp2p/webrtc'
import { webSockets } from '@libp2p/websockets'
import * as filters from '@libp2p/websockets/filters'
import { multiaddr } from '@multiformats/multiaddr'
import { createLibp2p } from 'libp2p'
import { fromString, toString } from 'uint8arrays'
const DOM = {
peerId: () => document.getElementById('peer-id'),
dialMultiaddrInput: () => document.getElementById('dial-multiaddr-input'),
dialMultiaddrButton: () => document.getElementById('dial-multiaddr-button'),
subscribeTopicInput: () => document.getElementById('subscribe-topic-input'),
subscribeTopicButton: () => document.getElementById('subscribe-topic-button'),
sendTopicMessageInput: () => document.getElementById('send-topic-message-input'),
sendTopicMessageButton: () => document.getElementById('send-topic-message-button'),
output: () => document.getElementById('output'),
listeningAddressesList: () => document.getElementById('listening-addresses'),
peerConnectionsList: () => document.getElementById('peer-connections'),
topicPeerList: () => document.getElementById('topic-peers')
}
const appendOutput = (line) => {
DOM.output().innerText += `${line}\n`
}
const clean = (line) => line.replaceAll('\n', '')
const libp2p = await createLibp2p({
addresses: {
listen: [
// make a reservation on any discovered relays - this will let other
// peers use the relay to contact us
'/p2p-circuit',
// create listeners for incoming WebRTC connection attempts on on all
// available Circuit Relay connections
'/webrtc'
]
},
transports: [
// the WebSocket transport lets us dial a local relay
webSockets({
// this allows non-secure WebSocket connections for purposes of the demo
filter: filters.all
}),
// support dialing/listening on WebRTC addresses
webRTC(),
// support dialing/listening on Circuit Relay addresses
circuitRelayTransport()
],
// a connection encrypter is necessary to dial the relay
connectionEncrypters: [noise()],
// a stream muxer is necessary to dial the relay
streamMuxers: [yamux()],
connectionGater: {
denyDialMultiaddr: () => {
// by default we refuse to dial local addresses from browsers since they
// are usually sent by remote peers broadcasting undialable multiaddrs and
// cause errors to appear in the console but in this example we are
// explicitly connecting to a local node so allow all addresses
return false
}
},
services: {
identify: identify(),
pubsub: gossipsub(),
dcutr: dcutr()
}
})
DOM.peerId().innerText = libp2p.peerId.toString()
function updatePeerList () {
// Update connections list
const peerList = libp2p.getPeers()
.map(peerId => {
const el = document.createElement('li')
el.textContent = peerId.toString()
const addrList = document.createElement('ul')
for (const conn of libp2p.getConnections(peerId)) {
const addr = document.createElement('li')
addr.textContent = conn.remoteAddr.toString()
addrList.appendChild(addr)
}
el.appendChild(addrList)
return el
})
DOM.peerConnectionsList().replaceChildren(...peerList)
}
// update peer connections
libp2p.addEventListener('connection:open', () => {
updatePeerList()
})
libp2p.addEventListener('connection:close', () => {
updatePeerList()
})
// update listening addresses
libp2p.addEventListener('self:peer:update', () => {
const multiaddrs = libp2p.getMultiaddrs()
.map((ma) => {
const el = document.createElement('li')
el.textContent = ma.toString()
return el
})
DOM.listeningAddressesList().replaceChildren(...multiaddrs)
})
// dial remote peer
DOM.dialMultiaddrButton().onclick = async () => {
const ma = multiaddr(DOM.dialMultiaddrInput().value)
appendOutput(`Dialing '${ma}'`)
await libp2p.dial(ma)
appendOutput(`Connected to '${ma}'`)
}
// subscribe to pubsub topic
DOM.subscribeTopicButton().onclick = async () => {
const topic = DOM.subscribeTopicInput().value
appendOutput(`Subscribing to '${clean(topic)}'`)
libp2p.services.pubsub.subscribe(topic)
DOM.sendTopicMessageInput().disabled = undefined
DOM.sendTopicMessageButton().disabled = undefined
}
// send message to topic
DOM.sendTopicMessageButton().onclick = async () => {
const topic = DOM.subscribeTopicInput().value
const message = DOM.sendTopicMessageInput().value
appendOutput(`Sending message '${clean(message)}'`)
await libp2p.services.pubsub.publish(topic, fromString(message))
}
// update topic peers
setInterval(() => {
const topic = DOM.subscribeTopicInput().value
const peerList = libp2p.services.pubsub.getSubscribers(topic)
.map(peerId => {
const el = document.createElement('li')
el.textContent = peerId.toString()
return el
})
DOM.topicPeerList().replaceChildren(...peerList)
}, 500)
libp2p.services.pubsub.addEventListener('message', event => {
const topic = event.detail.topic
const message = toString(event.detail.data)
appendOutput(`Message received on topic '${topic}'`)
appendOutput(message)
})