docs: update auto relay example code usage (#1163)

Co-authored-by: aomini daiki <rakesh.shrestha@hazesoft.co>
This commit is contained in:
Rakesh Shrestha 2022-02-28 22:31:56 +05:45 committed by GitHub
parent 6a3e37f250
commit 3cfe4bbfac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -106,18 +106,15 @@ console.log(`Node started with id ${node.peerId.toB58String()}`)
const conn = await node.dial(relayAddr)
// Wait for connection and relay to be bind for the example purpose
await new Promise((resolve) => {
node.peerStore.on('change:multiaddrs', ({ peerId }) => {
// Updated self multiaddrs?
if (peerId.equals(node.peerId)) {
resolve()
}
})
})
console.log(`Connected to the HOP relay ${conn.remotePeer.toString()}`)
console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`)
// Wait for connection and relay to be bind for the example purpose
node.peerStore.on('change:multiaddrs', ({ peerId }) => {
// Updated self multiaddrs?
if (peerId.equals(node.peerId)) {
console.log(`Advertising with a relay address of ${node.multiaddrs[0].toString()}/p2p/${node.peerId.toB58String()}`)
}
})
```
As you can see in the code, we need to provide the relay address, `relayAddr`, as a process argument. This node will dial the provided relay address and automatically bind to it.
@ -189,4 +186,4 @@ As you can see from the output, the remote address of the established connection
Before moving into production, there are a few things that you should take into account.
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 browsers 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.
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 browsers 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.