Skip to content

Commit 20a2e1e

Browse files
authored
docs: update relay example to use current terminology (#203)
Updates the example to reflect current terminology. Replace references to "auto relay" to just "relay address" Adds section about configuring auto-tls as an alternative to manually setting up nginx/SSL
1 parent 88897fc commit 20a2e1e

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

examples/js-libp2p-example-circuit-relay/README.md

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const node = await createLibp2p({
5151
addresses: {
5252
listen: ['/ip4/0.0.0.0/tcp/0/ws']
5353
// TODO check "What is next?" section
54-
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
5554
},
5655
transports: [
5756
webSockets()
@@ -148,7 +147,8 @@ As you can see in the code, we need to provide the relay address, `relayAddr`,
148147
as a process argument. This node will dial the provided relay address and
149148
automatically bind to it.
150149

151-
You should now run the following to start the node running Auto Relay:
150+
You should now run the following to start the listener and see it automatically
151+
acquire a circuit relay address:
152152

153153
```sh
154154
node listener.js /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
@@ -159,32 +159,37 @@ This should print out something similar to the following:
159159
```sh
160160
Node started with id QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
161161
Connected to the HOP relay QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
162-
Advertising with a relay address of /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
162+
Listening on a relay address of /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
163163
```
164164

165-
Per the address, it is possible to verify that the auto relay node is listening
166-
on the circuit relay node address.
165+
Per the listening address, it is possible to verify that the listener node is
166+
indeed listening on the circuit relay node address.
167167

168168
Instead of dialing this relay manually, you could set up this node with the
169-
Bootstrap module and provide it in the bootstrap list. Moreover, you can use
170-
other `peer-discovery` modules to discover peers in the network and the node
171-
will automatically bind to the relays that support HOP until reaching the
172-
maximum number of listeners.
169+
`@libp2p/bootstrap` module and provide it in the bootstrap list.
170+
171+
Alternatively, you can use other `peer-discovery` modules such as
172+
`@libp2p/kad-dht` which allow your node to perform a random walk of the network
173+
to discover peers running the Circuit Relay HOP protocol and the node will
174+
automatically bind to these relays until reaching the maximum number of
175+
listeners defined by how many `/p2p-circuit` entries in the `address.listen`
176+
array (usually one is sufficient).
173177

174178
## 3. Set up a dialer node for testing connectivity
175179

176180
Now that you have a relay node and a node bound to that relay, you can test
177-
connecting to the auto relay node via the relay.
181+
connecting to the listening node via the relay.
178182

179183
```js
180184
import { createLibp2p } from 'libp2p'
181185
import { webSockets } from '@libp2p/websockets'
182186
import { noise } from '@chainsafe/libp2p-noise'
183-
import { yamux } from '@chainsafe/libp2p-yamux',
187+
import { yamux } from '@chainsafe/libp2p-yamux'
188+
import { multiaddr } from '@multiformats/multiaddr'
184189

185-
const autoRelayNodeAddr = process.argv[2]
186-
if (!autoRelayNodeAddr) {
187-
throw new Error('the auto relay node address needs to be specified')
190+
const listenNodeAddr = process.argv[2]
191+
if (!listenNodeAddr) {
192+
throw new Error('The listening node address needs to be specified')
188193
}
189194

190195
const node = await createLibp2p({
@@ -195,23 +200,26 @@ const node = await createLibp2p({
195200

196201
console.log(`Node started with id ${node.peerId.toString()}`)
197202

198-
const conn = await node.dial(autoRelayNodeAddr)
199-
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
203+
const ma = multiaddr(listenNodeAddr)
204+
const conn = await node.dial(ma, {
205+
signal: AbortSignal.timeout(10_000)
206+
})
207+
console.log(`Connected to the listen node via ${conn.remoteAddr.toString()}`)
200208
```
201209

202210
You should now run the following to start the relay node using the listen
203211
address from step 2:
204212

205213
```sh
206-
node dialer.js /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
214+
node dialer.js /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
207215
```
208216

209217
Once you start your test node, it should print out something similar to the
210218
following:
211219

212220
```sh
213221
Node started: Qme7iEzDxFoFhhkrsrkHkMnM11aPYjysaehP4NZeUfVMKG
214-
Connected to the auto relay node via /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
222+
Connected to the listening node via /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
215223
```
216224

217225
As you can see from the output, the remote address of the established connection
@@ -223,12 +231,20 @@ Before moving into production, there are a few things that you should take into
223231
account.
224232

225233
A relay node should not advertise its private address in a real world scenario,
226-
as the node would not be reachable by others. You should provide an array of
227-
public addresses in the libp2p `addresses.announce` option. If you are using
228-
websockets, bear in mind that due to browser’s security policies you cannot
229-
establish unencrypted connection from secure context. The simplest solution is
230-
to setup SSL with nginx and proxy to the node and setup a domain name for the
231-
certificate.
234+
as the node would not be reachable by others.
235+
236+
If you are using websockets, bear in mind that due to browser’s security
237+
policies you cannot establish unencrypted connection from secure context.
238+
239+
One solution is to setup TLS with nginx and proxy to the node and setup
240+
a domain name for the certificate. You can then provide an list of public
241+
addresses in the libp2p `addresses.announce` config option.
242+
243+
Alternatively you can use the public [AutoTLS service](https://blog.libp2p.io/autotls/)
244+
(provided by [Interplanetary Shipyard](https://blog.ipfs.tech/shipyard-hello-world/))
245+
to automatically provision a TLS certificate and accompanying domain name - see
246+
the [js-libp2p-example-auto-tls](https://github.com/libp2p/js-libp2p-example-auto-tls)
247+
for more information.
232248

233249
## Need help?
234250

examples/js-libp2p-example-circuit-relay/dialer.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { multiaddr } from '@multiformats/multiaddr'
99
import { createLibp2p } from 'libp2p'
1010

1111
async function main () {
12-
const autoRelayNodeAddr = process.argv[2]
13-
if (!autoRelayNodeAddr) {
14-
throw new Error('the auto relay node address needs to be specified')
12+
const listenNodeAddr = process.argv[2]
13+
if (!listenNodeAddr) {
14+
throw new Error('The listening node address needs to be specified')
1515
}
1616

1717
const node = await createLibp2p({
@@ -32,8 +32,11 @@ async function main () {
3232

3333
console.log(`Node started with id ${node.peerId.toString()}`)
3434

35-
const conn = await node.dial(multiaddr(autoRelayNodeAddr))
36-
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
35+
const ma = multiaddr(listenNodeAddr)
36+
const conn = await node.dial(ma, {
37+
signal: AbortSignal.timeout(10_000)
38+
})
39+
console.log(`Connected to the listening node via ${conn.remoteAddr.toString()}`)
3740
}
3841

3942
main()

examples/js-libp2p-example-circuit-relay/listener.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function main () {
4444
// Wait for connection and relay to be bind for the example purpose
4545
node.addEventListener('self:peer:update', (evt) => {
4646
// Updated self multiaddrs?
47-
console.log(`Advertising with a relay address of ${node.getMultiaddrs()[0].toString()}`)
47+
console.log(`Listening on a relay address of ${node.getMultiaddrs()[0].toString()}`)
4848
})
4949
}
5050

examples/js-libp2p-example-circuit-relay/relay.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ async function main () {
1212
addresses: {
1313
listen: ['/ip4/0.0.0.0/tcp/0/ws']
1414
// TODO check "What is next?" section
15-
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
1615
},
1716
transports: [
1817
webSockets()

examples/js-libp2p-example-circuit-relay/test/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ process.stdout.write('listener.js\n')
2020
const {
2121
process: listener,
2222
matches: [, autoRelayAddr]
23-
} = await matchOutput(/^Advertising with a relay address of (\/ip4\/.*)$/m, 'node', [path.resolve(__dirname, '../listener.js'), relayAddress])
23+
} = await matchOutput(/^Listening on a relay address of (\/ip4\/.*)$/m, 'node', [path.resolve(__dirname, '../listener.js'), relayAddress])
2424

2525
process.stdout.write('==================================================================\n')
2626

2727
// Step 3 process
2828
process.stdout.write('dialer.js\n')
2929

30-
await waitForOutput(`Connected to the auto relay node via ${autoRelayAddr}`, 'node', [path.resolve(__dirname, '../dialer.js'), autoRelayAddr])
30+
await waitForOutput(`Connected to the listening node via ${autoRelayAddr}`, 'node', [path.resolve(__dirname, '../dialer.js'), autoRelayAddr])
3131

3232
listener.kill()
3333
relay.kill()

0 commit comments

Comments
 (0)