Skip to content

Commit fd049f1

Browse files
committed
refactor: examples-delegated-routing
1 parent 02910fd commit fd049f1

File tree

4 files changed

+44
-56
lines changed

4 files changed

+44
-56
lines changed

examples/delegated-routing/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ especially useful when your libp2p node will have limited resources, making runn
66
also highly useful if your node is generating content, but can't reliably be on the network. You can use delegate nodes
77
to provide content on your behalf.
88

9-
The starting [Libp2p Bundle](./src/libp2p-bundle.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
9+
The starting [Libp2p Configuration](./src/libp2p-configuration.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
1010
Once you've completed the example, you should try enabled the DHT and see what kind of results you get! You can also enable the
1111
various Peer Discovery modules and see the impact it has on your Peer count.
1212

@@ -19,7 +19,9 @@ various Peer Discovery modules and see the impact it has on your Peer count.
1919
2. Run the IPFS daemon: `ipfs daemon`
2020
3. The daemon will output a line about its API address, like `API server listening on /ip4/127.0.0.1/tcp/8080`
2121
4. In another window output the addresses of the node: `ipfs id`. Make note of the websocket address, it will contain `/ws/` in the address.
22-
5. In `./src/libp2p-bundle.js` check if the host and port of your node are correct, according to the previous step. If they are different, replace them.
22+
- If there is no websocket address, you will need to add it in the ipfs config file (`~/.ipfs/config`)
23+
- Add to Swarm Addresses something like: `"/ip4/127.0.0.1/tcp/4010/ws"`
24+
5. In `./src/libp2p-configuration.js` check if the host and port of your node are correct, according to the previous step. If they are different, replace them.
2325
6. In `./src/App.js` replace `BootstrapNode` with your nodes Websocket address from step 4.
2426
7. Start this example:
2527

examples/delegated-routing/package.json

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"ipfs": "~0.34.4",
7-
"libp2p": "github:libp2p/js-libp2p#master",
8-
"libp2p-delegated-content-routing": "~0.2.2",
9-
"libp2p-delegated-peer-routing": "~0.2.2",
10-
"libp2p-kad-dht": "~0.14.12",
11-
"libp2p-mplex": "~0.8.5",
12-
"libp2p-secio": "~0.11.1",
13-
"libp2p-webrtc-star": "~0.15.8",
14-
"libp2p-websocket-star": "~0.10.2",
15-
"libp2p-websockets": "~0.12.2",
16-
"react": "^16.8.6",
17-
"react-dom": "^16.8.6",
18-
"react-scripts": "2.1.8"
6+
"ipfs": "~0.40.0",
7+
"libp2p": "../..",
8+
"libp2p-delegated-content-routing": "~0.4.1",
9+
"libp2p-delegated-peer-routing": "~0.4.0",
10+
"libp2p-kad-dht": "~0.18.2",
11+
"libp2p-mplex": "~0.9.3",
12+
"libp2p-secio": "~0.12.1",
13+
"libp2p-webrtc-star": "~0.17.0",
14+
"libp2p-websockets": "~0.13.1",
15+
"react": "^16.12.0",
16+
"react-dom": "^16.12.0",
17+
"react-scripts": "3.3.0"
1918
},
2019
"scripts": {
2120
"start": "react-scripts start"

examples/delegated-routing/src/App.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
import React from 'react'
55
import Ipfs from 'ipfs'
6-
import libp2pBundle from './libp2p-bundle'
7-
const Component = React.Component
6+
import libp2pConfig from './libp2p-configuration'
87

9-
const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/p2p/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
8+
const BootstrapNode = '/ip4/127.0.0.1/tcp/4004/ws/ipfs/QmPHafDaco9vynQ93MHv5cRSW6UCECycCGdTRafL8X5WEj'
109

11-
class App extends Component {
10+
class App extends React.Component {
1211
constructor (props) {
1312
super(props)
1413
this.state = {
@@ -38,34 +37,30 @@ class App extends Component {
3837
})
3938
}
4039

41-
handleHashSubmit (event) {
40+
async handleHashSubmit (event) {
4241
event.preventDefault()
4342
this.setState({
4443
isLoading: this.state.isLoading + 1
4544
})
4645

47-
this.ipfs.cat(this.state.hash, (err, data) => {
48-
if (err) console.log('Error', err)
46+
const data = await this.ipfs.cat(this.state.hash)
4947

50-
this.setState({
51-
response: data.toString(),
52-
isLoading: this.state.isLoading - 1
53-
})
48+
this.setState({
49+
response: data.toString(),
50+
isLoading: this.state.isLoading - 1
5451
})
5552
}
56-
handlePeerSubmit (event) {
53+
async handlePeerSubmit (event) {
5754
event.preventDefault()
5855
this.setState({
5956
isLoading: this.state.isLoading + 1
6057
})
6158

62-
this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
63-
if (err) console.log('Error', err)
59+
const results = await this.ipfs.dht.findpeer(this.state.peer)
6460

65-
this.setState({
66-
response: JSON.stringify(results, null, 2),
67-
isLoading: this.state.isLoading - 1
68-
})
61+
this.setState({
62+
response: JSON.stringify(results, null, 2),
63+
isLoading: this.state.isLoading - 1
6964
})
7065
}
7166

@@ -90,25 +85,22 @@ class App extends Component {
9085
preload: {
9186
enabled: false
9287
},
93-
libp2p: libp2pBundle
88+
libp2p: libp2pConfig
9489
})
95-
this.ipfs.on('ready', () => {
90+
91+
this.ipfs.on('ready', async () => {
9692
if (this.peerInterval) {
9793
clearInterval(this.peerInterval)
9894
}
9995

100-
this.ipfs.swarm.connect(BootstrapNode, (err) => {
101-
if (err) {
102-
console.log('Error connecting to the node', err)
103-
}
104-
console.log('Connected!')
105-
})
106-
107-
this.peerInterval = setInterval(() => {
108-
this.ipfs.swarm.peers((err, peers) => {
109-
if (err) console.log(err)
110-
if (peers) this.setState({peers: peers.length})
111-
})
96+
97+
await this.ipfs.swarm.connect(BootstrapNode)
98+
console.log('Connected!')
99+
100+
this.peerInterval = setInterval(async () => {
101+
const peers = await this.ipfs.swarm.peers()
102+
103+
if (peers) this.setState({peers: peers.length})
112104
}, 2500)
113105
})
114106
}

examples/delegated-routing/src/libp2p-bundle.js renamed to examples/delegated-routing/src/libp2p-configuration.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,23 @@
33

44
const Libp2p = require('libp2p')
55
const Websockets = require('libp2p-websockets')
6-
const WebSocketStar = require('libp2p-websocket-star')
76
const WebRTCStar = require('libp2p-webrtc-star')
87
const MPLEX = require('libp2p-mplex')
98
const SECIO = require('libp2p-secio')
109
const KadDHT = require('libp2p-kad-dht')
1110
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
1211
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
1312

14-
export default function Libp2pBundle ({peerInfo, peerBook}) {
13+
export default function Libp2pConfiguration ({peerInfo}) {
1514
const wrtcstar = new WebRTCStar({id: peerInfo.id})
16-
const wsstar = new WebSocketStar({id: peerInfo.id})
1715
const delegatedApiOptions = {
18-
host: '0.0.0.0',
16+
host: '127.0.0.1',
1917
protocol: 'http',
20-
port: '8080'
18+
port: '5001'
2119
}
2220

2321
return new Libp2p({
2422
peerInfo,
25-
peerBook,
2623
// Lets limit the connection managers peers and have it check peer health less frequently
2724
connectionManager: {
2825
maxPeers: 10,
@@ -36,12 +33,10 @@ export default function Libp2pBundle ({peerInfo, peerBook}) {
3633
new DelegatedPeerRouter(delegatedApiOptions)
3734
],
3835
peerDiscovery: [
39-
wrtcstar.discovery,
40-
wsstar.discovery
36+
wrtcstar.discovery
4137
],
4238
transport: [
4339
wrtcstar,
44-
wsstar,
4540
Websockets
4641
],
4742
streamMuxer: [

0 commit comments

Comments
 (0)