You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/auto-relay/README.md
+60-31Lines changed: 60 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
# Auto relay
2
2
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).
4
4
While direct connections to nodes are preferable, it's not always possible to do so due to NATs or browser limitations.
5
5
6
6
## 0. Setup the example
7
7
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.
9
9
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.
11
11
12
12
## 1. Set up a relay node
13
13
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.
15
15
16
16
The relay node will need to have its relay subsystem enabled, as well as its HOP capability. It can be configured as follows:
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.
55
57
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:
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:
70
78
71
79
```js
72
80
constLibp2p=require('libp2p')
73
81
constWebsockets=require('libp2p-websockets')
74
82
const { NOISE } =require('libp2p-noise')
75
83
constMPLEX=require('libp2p-mplex')
76
84
77
-
// TODO: get the relay address from the previous step
78
-
constrelayAddr=undefined
85
+
constpWaitFor=require('p-wait-for')
86
+
87
+
constrelayAddr=process.argv[2]
88
+
if (!relayAddr) {
89
+
thrownewError('the relay address needs to be specified as a parameter')
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.
109
123
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:
Per the address, it is possible to verify that the auto relay node is listening on the circuit relay node address.
120
140
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.
122
142
123
143
## 3. Set up another node for testing connectivity
124
144
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.
126
146
127
147
```js
128
148
constLibp2p=require('libp2p')
129
149
constWebsockets=require('libp2p-websockets')
130
150
const { NOISE } =require('libp2p-noise')
131
151
constMPLEX=require('libp2p-mplex')
132
152
133
-
// TODO: get the auto relay address from the previous step
134
-
constautoRelayNodeAddr=undefined
153
+
constautoRelayNodeAddr=process.argv[2]
154
+
if (!autoRelayNodeAddr) {
155
+
thrownewError('the auto relay node address needs to be specified')
Once you start your test node, it should print out something similar to the following:
153
180
154
181
```sh
155
-
Connected to the auto relay node via /ip4/192.168.1.120/tcp/61470/ws/p2p/Qme1DfXDeaMEPNsUrG8EFXj2JDqzpgy9LuD6mpqpBsNwTm/p2p-circuit/p2p/Qmch46oemLTk6HJX1Yzm8gVRLPvBStoMQNniB37mX34RqM
Connected to the auto relay node via /ip4/192.168.1.120/tcp/61592/ws/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3/p2p-circuit/p2p/QmerrWofKF358JE6gv3z74cEAyL7z1KqhuUoVfGEynqjRm
156
184
```
157
185
186
+
As you can see from the output, the remote address of the established connection uses the relayed connection.
187
+
158
188
## 4. What is next?
159
189
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.
0 commit comments