Skip to content

Commit dfceafb

Browse files
committed
chore: update examples to use process arguments
1 parent cd06602 commit dfceafb

File tree

4 files changed

+73
-45
lines changed

4 files changed

+73
-45
lines changed

examples/auto-relay/README.md

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Auto relay
22

3-
Auto Relay enables libp2p nodes to dynamically find and bind to relays on the network. Once binding (listening) is done, the node can and should advertise its addresses to the network, allowing any other node to dial it over its bound relay(s).
3+
Auto Relay enables libp2p nodes to dynamically find and bind to relays on the network. Once binding (listening) is done, the node can and should advertise its addresses on the network, allowing any other node to dial it over its bound relay(s).
44
While direct connections to nodes are preferable, it's not always possible to do so due to NATs or browser limitations.
55

66
## 0. Setup the example
77

8-
Before moving into the examples, you should run `npm install` on the top level folder of libp2p, in order to install all the dependencies needed for these examples.
8+
Before moving into the examples, you should run `npm install` on the top level `js-libp2p` folder, in order to install all the dependencies needed for this example.
99

10-
This example comes with 3 main files to run. A `relay.js` file to be used in the first step, a `auto-relay.js` file to be used in the second step and a `other-node.js` file to be used on the third step. All of this scripts will run their own libp2p node, which will interact with the previous ones. This way, you need to have all of them running as you proceed.
10+
This example comes with 3 main files. A `relay.js` file to be used in the first step, a `auto-relay.js` file to be used in the second step and a `other-node.js` file to be used on the third step. All of this scripts will run their own libp2p node, which will interact with the previous ones. This way, you need to have all of them running as you proceed.
1111

1212
## 1. Set up a relay node
1313

14-
Aiming to support nodes with connectivity difficulties, you will need to set up a relay node for the former nodes to bind.
14+
Aiming to support nodes with connectivity issues, you will need to set up a relay node for the former nodes to bind.
1515

1616
The relay node will need to have its relay subsystem enabled, as well as its HOP capability. It can be configured as follows:
1717

@@ -29,7 +29,9 @@ const node = await Libp2p.create({
2929
},
3030
addresses: {
3131
listen: ['/ip4/0.0.0.0/tcp/0/ws']
32-
// announceFilter: TODO check "What is next?" section
32+
// TODO check "What is next?" section
33+
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
34+
// announceFilter: (addresses) => addresses
3335
},
3436
config: {
3537
relay: {
@@ -46,36 +48,46 @@ const node = await Libp2p.create({
4648

4749
await node.start()
4850

49-
console.log(`Node started. ${node.peerId.toB58String()}`)
51+
console.log(`Node started: ${node.peerId.toB58String()}`)
5052
console.log('Listening on:')
5153
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
5254
```
5355

5456
The Relay HOP advertise functionality is **NOT** required to be enabled. However, if you are interested in advertising on the network that this node is available to be used as a HOP Relay you can enable it.
5557

56-
Once you start your relay node with `node relay.js`, it should print out something similar to the following:
58+
You should now run the following to start the relay node:
5759

5860
```sh
59-
Node started. QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
61+
node relay.js
62+
```
63+
64+
This should print out something similar to the following:
65+
66+
```sh
67+
Node started: QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
6068
Listening on:
61-
/ip4/127.0.0.1/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
62-
/ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
69+
/ip4/127.0.0.1/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
70+
/ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3
6371
```
6472

6573
TODO: Docker Image with a repo
6674

6775
## 2. Set up a node with Auto Relay Enabled
6876

69-
One of the typical use cases for Auto Relay is nodes behind a NAT or browser nodes thanks to their limitations regarding listening for new connections.
77+
One of the typical use cases for Auto Relay is nodes behind a NAT or browser nodes thanks to their limitations regarding listening for new connections. For running a libp2p node that automatically binds itself to connected HOP relays, you can see the following:
7078

7179
```js
7280
const Libp2p = require('libp2p')
7381
const Websockets = require('libp2p-websockets')
7482
const { NOISE } = require('libp2p-noise')
7583
const MPLEX = require('libp2p-mplex')
7684

77-
// TODO: get the relay address from the previous step
78-
const relayAddr = undefined
85+
const pWaitFor = require('p-wait-for')
86+
87+
const relayAddr = process.argv[2]
88+
if (!relayAddr) {
89+
throw new Error('the relay address needs to be specified as a parameter')
90+
}
7991

8092
const node = await Libp2p.create({
8193
modules: {
@@ -95,43 +107,53 @@ const node = await Libp2p.create({
95107
})
96108

97109
await node.start()
98-
console.log(`Node started. ${node.peerId.toB58String()}`)
99-
console.log('Listening on:')
100-
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
110+
console.log(`Node started: ${node.peerId.toB58String()}`)
101111

102112
await node.dial(relayAddr)
113+
114+
// Wait for connection and relay to be bind for the example purpose
115+
await pWaitFor(() => node.multiaddrs.length > 0)
116+
103117
console.log('connected to the HOP relay')
104118
console.log('Listening on:')
105119
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
106120
```
107121

108-
Before starting your node leveraging auto relay, you need to fill in the `relayAddr` in the code with the relay listening address from step 1.
122+
As you can see in the code, we need to provide the `relayAddr` as a process argument. This node will dial the relay and automatically bind to the relay.
109123

110-
Once you start your auto relay node with `node auto-relay.js`, it should print out something similar to the following:
124+
You should now run the following to start the relay node:
111125

112126
```sh
113-
Node started. QmSmhZ1pTV5ox7DUfG8QPSwyNyXGsWUeTCEWXfH7MVXLfi
127+
node auto-relay.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
128+
```
129+
130+
This should print out something similar to the following:
131+
132+
```sh
133+
Node started: QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
114134
connected to the HOP relay
115135
Listening on:
116-
/ip4/192.168.1.120/tcp/60288/ws/p2p/QmNusKcZR1WNKEJqvPeKtPfzHxAviqH5P2RxyKRqynV6WD/p2p-circuit/p2p/QmSmhZ1pTV5ox7DUfG8QPSwyNyXGsWUeTCEWXfH7MVXLfi
136+
/ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
117137
```
118138

119139
Per the address, it is possible to verify that the auto relay node is listening on the circuit relay node address.
120140

121-
Instead of dialing this relay manually, you could set up this node with the Bootstrap module and provide it in the bootstrap list.
141+
Instead of dialing this relay manually, you could set up this node with the Bootstrap module and provide it in the bootstrap list. Moreover, you can use other `peer-discovery` modules to discover peers in the network and the node will automatically bind to the relays that support HOP until reaching the maximum number of listeners.
122142

123143
## 3. Set up another node for testing connectivity
124144

125-
Now that you have set up a relay node and a node leveraging that relay with auto relay, you can test connecting to the auto relay node via the relay.
145+
Now that you have a relay node and a node bound to that relay, you can test connecting to the auto relay node via the relay.
126146

127147
```js
128148
const Libp2p = require('libp2p')
129149
const Websockets = require('libp2p-websockets')
130150
const { NOISE } = require('libp2p-noise')
131151
const MPLEX = require('libp2p-mplex')
132152

133-
// TODO: get the auto relay address from the previous step
134-
const autoRelayNodeAddr = undefined
153+
const autoRelayNodeAddr = process.argv[2]
154+
if (!autoRelayNodeAddr) {
155+
throw new Error('the auto relay node address needs to be specified')
156+
}
135157

136158
const node = await Libp2p.create({
137159
modules: {
@@ -142,22 +164,29 @@ const node = await Libp2p.create({
142164
})
143165

144166
await node.start()
167+
console.log(`Node started: ${node.peerId.toB58String()}`)
145168

146169
const conn = await node.dial(autoRelayNodeAddr)
147170
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)
148171
```
149172

150-
Before starting your node leveraging auto relay, you need to fill in the `autoRelayNodeAddr` in the code with the relay listening address from step 2.
173+
You should now run the following to start the relay node using the listen address from step 2:
151174

152-
Once you start your test node with `node other-relay.js`, it should print out something similar to the following:
175+
```sh
176+
node other-node.js /ip4/192.168.1.120/tcp/58941/ws/p2p/QmQKCBm87HQMbFqy14oqC85pMmnRrj6iD46ggM6reqNpsd
177+
```
178+
179+
Once you start your test node, it should print out something similar to the following:
153180

154181
```sh
155-
Connected to the auto relay node via /ip4/192.168.1.120/tcp/61470/ws/p2p/Qme1DfXDeaMEPNsUrG8EFXj2JDqzpgy9LuD6mpqpBsNwTm/p2p-circuit/p2p/Qmch46oemLTk6HJX1Yzm8gVRLPvBStoMQNniB37mX34RqM
182+
Node started: Qme7iEzDxFoFhhkrsrkHkMnM11aPYjysaehP4NZeUfVMKG
183+
Connected to the auto relay node via /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
156184
```
157185

186+
As you can see from the output, the remote address of the established connection uses the relayed connection.
187+
158188
## 4. What is next?
159189

160-
- Private addr
161-
- Use `webrtc-star` for discovering other peers (will get both announced addresses that might be used on peer exchange/DHT queries)
162-
- Check libp2p in the browser example...
163-
- Infra guide?
190+
Before moving into production, there are a few things that you should take into account.
191+
192+
A relay node should not advertise its private address in a real world scenario, as the node would not be reachable by others. You should provide an array of public addresses in the libp2p `addresses.announce` option. If you are using websockets, bear in mind that due to browser’s security policies you cannot establish unencrypted connection from secure context. The simplest solution is to setup SSL with nginx and proxy to the node and setup a domain name for the certificate.

examples/auto-relay/auto-relay.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ const MPLEX = require('libp2p-mplex')
77

88
const pWaitFor = require('p-wait-for')
99

10-
// TODO: get the relay address from the previous step)
11-
const relayAddr = '/ip4/192.168.1.120/tcp/61470/ws/p2p/Qme1DfXDeaMEPNsUrG8EFXj2JDqzpgy9LuD6mpqpBsNwTm'
10+
const relayAddr = process.argv[2]
1211
if (!relayAddr) {
13-
throw new Error('the relay address needs to be specified')
12+
throw new Error('the relay address needs to be specified as a parameter')
1413
}
1514

16-
; (async () => {
15+
;(async () => {
1716
const node = await Libp2p.create({
1817
modules: {
1918
transport: [Websockets],
@@ -32,11 +31,11 @@ if (!relayAddr) {
3231
})
3332

3433
await node.start()
35-
console.log(`Node started. ${node.peerId.toB58String()}`)
34+
console.log(`Node started: ${node.peerId.toB58String()}`)
3635

3736
await node.dial(relayAddr)
3837

39-
// Wait for connection and relay to be bind
38+
// Wait for connection and relay to be bind for the example purpose
4039
await pWaitFor(() => node.multiaddrs.length > 0)
4140

4241
console.log('connected to the HOP relay')

examples/auto-relay/other-node.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ const Websockets = require('libp2p-websockets')
55
const { NOISE } = require('libp2p-noise')
66
const MPLEX = require('libp2p-mplex')
77

8-
;(async () => {
9-
// TODO: get the auto relay address from the previous step
10-
const autoRelayNodeAddr = '/ip4/192.168.1.120/tcp/61470/ws/p2p/Qme1DfXDeaMEPNsUrG8EFXj2JDqzpgy9LuD6mpqpBsNwTm/p2p-circuit/p2p/Qmch46oemLTk6HJX1Yzm8gVRLPvBStoMQNniB37mX34RqM'
11-
if (!autoRelayNodeAddr) {
12-
throw new Error('the auto relay node address needs to be specified')
13-
}
8+
const autoRelayNodeAddr = process.argv[2]
9+
if (!autoRelayNodeAddr) {
10+
throw new Error('the auto relay node address needs to be specified')
11+
}
1412

13+
;(async () => {
1514
const node = await Libp2p.create({
1615
modules: {
1716
transport: [Websockets],
@@ -21,6 +20,7 @@ const MPLEX = require('libp2p-mplex')
2120
})
2221

2322
await node.start()
23+
console.log(`Node started: ${node.peerId.toB58String()}`)
2424

2525
const conn = await node.dial(autoRelayNodeAddr)
2626
console.log(`Connected to the auto relay node via ${conn.remoteAddr.toString()}`)

examples/auto-relay/relay.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const MPLEX = require('libp2p-mplex')
3131

3232
await node.start()
3333

34-
console.log(`Node started. ${node.peerId.toB58String()}`)
34+
console.log(`Node started: ${node.peerId.toB58String()}`)
3535
console.log('Listening on:')
3636
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
37-
})()
37+
})()

0 commit comments

Comments
 (0)