Skip to content

Commit

Permalink
refactor: examples-delegated-routing
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 16, 2019
1 parent 56a1825 commit 0f13e53
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 56 deletions.
6 changes: 4 additions & 2 deletions examples/delegated-routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ especially useful when your libp2p node will have limited resources, making runn
also highly useful if your node is generating content, but can't reliably be on the network. You can use delegate nodes
to provide content on your behalf.

The starting [Libp2p Bundle](./src/libp2p-bundle.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
The starting [Libp2p Configuration](./src/libp2p-configuration.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
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
various Peer Discovery modules and see the impact it has on your Peer count.

Expand All @@ -19,7 +19,9 @@ various Peer Discovery modules and see the impact it has on your Peer count.
2. Run the IPFS daemon: `ipfs daemon`
3. The daemon will output a line about its API address, like `API server listening on /ip4/127.0.0.1/tcp/8080`
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.
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.
- If there is no websocket address, you will need to add it in the ipfs config file (`~/.ipfs/config`)
- Add to Swarm Addresses something like: `"/ip4/127.0.0.1/tcp/4010/ws"`
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.
6. In `./src/App.js` replace `BootstrapNode` with your nodes Websocket address from step 4.
7. Start this example:

Expand Down
25 changes: 12 additions & 13 deletions examples/delegated-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"ipfs": "~0.34.4",
"libp2p": "github:libp2p/js-libp2p#master",
"libp2p-delegated-content-routing": "~0.2.2",
"libp2p-delegated-peer-routing": "~0.2.2",
"libp2p-kad-dht": "~0.14.12",
"libp2p-mplex": "~0.8.5",
"libp2p-secio": "~0.11.1",
"libp2p-webrtc-star": "~0.15.8",
"libp2p-websocket-star": "~0.10.2",
"libp2p-websockets": "~0.12.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "2.1.8"
"ipfs": "~0.40.0",
"libp2p": "../..",
"libp2p-delegated-content-routing": "~0.4.1",
"libp2p-delegated-peer-routing": "~0.4.0",
"libp2p-kad-dht": "~0.18.2",
"libp2p-mplex": "~0.9.3",
"libp2p-secio": "~0.12.1",
"libp2p-webrtc-star": "~0.17.0",
"libp2p-websockets": "~0.13.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.0"
},
"scripts": {
"start": "react-scripts start"
Expand Down
56 changes: 24 additions & 32 deletions examples/delegated-routing/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

import React from 'react'
import Ipfs from 'ipfs'
import libp2pBundle from './libp2p-bundle'
const Component = React.Component
import libp2pConfig from './libp2p-configuration'

const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/p2p/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
const BootstrapNode = '/ip4/127.0.0.1/tcp/4004/ws/ipfs/QmPHafDaco9vynQ93MHv5cRSW6UCECycCGdTRafL8X5WEj'

class App extends Component {
class App extends React.Component {
constructor (props) {
super(props)
this.state = {
Expand Down Expand Up @@ -38,34 +37,30 @@ class App extends Component {
})
}

handleHashSubmit (event) {
async handleHashSubmit (event) {
event.preventDefault()
this.setState({
isLoading: this.state.isLoading + 1
})

this.ipfs.cat(this.state.hash, (err, data) => {
if (err) console.log('Error', err)
const data = await this.ipfs.cat(this.state.hash)

this.setState({
response: data.toString(),
isLoading: this.state.isLoading - 1
})
this.setState({
response: data.toString(),
isLoading: this.state.isLoading - 1
})
}
handlePeerSubmit (event) {
async handlePeerSubmit (event) {
event.preventDefault()
this.setState({
isLoading: this.state.isLoading + 1
})

this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
if (err) console.log('Error', err)
const results = await this.ipfs.dht.findpeer(this.state.peer)

this.setState({
response: JSON.stringify(results, null, 2),
isLoading: this.state.isLoading - 1
})
this.setState({
response: JSON.stringify(results, null, 2),
isLoading: this.state.isLoading - 1
})
}

Expand All @@ -90,25 +85,22 @@ class App extends Component {
preload: {
enabled: false
},
libp2p: libp2pBundle
libp2p: libp2pConfig
})
this.ipfs.on('ready', () => {

this.ipfs.on('ready', async () => {
if (this.peerInterval) {
clearInterval(this.peerInterval)
}

this.ipfs.swarm.connect(BootstrapNode, (err) => {
if (err) {
console.log('Error connecting to the node', err)
}
console.log('Connected!')
})

this.peerInterval = setInterval(() => {
this.ipfs.swarm.peers((err, peers) => {
if (err) console.log(err)
if (peers) this.setState({peers: peers.length})
})

await this.ipfs.swarm.connect(BootstrapNode)
console.log('Connected!')

this.peerInterval = setInterval(async () => {
const peers = await this.ipfs.swarm.peers()

if (peers) this.setState({peers: peers.length})
}, 2500)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@

const Libp2p = require('libp2p')
const Websockets = require('libp2p-websockets')
const WebSocketStar = require('libp2p-websocket-star')
const WebRTCStar = require('libp2p-webrtc-star')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const KadDHT = require('libp2p-kad-dht')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')

export default function Libp2pBundle ({peerInfo, peerBook}) {
export default function Libp2pConfiguration ({peerInfo}) {
const wrtcstar = new WebRTCStar({id: peerInfo.id})
const wsstar = new WebSocketStar({id: peerInfo.id})
const delegatedApiOptions = {
host: '0.0.0.0',
host: '127.0.0.1',
protocol: 'http',
port: '8080'
port: '5001'
}

return new Libp2p({
peerInfo,
peerBook,
// Lets limit the connection managers peers and have it check peer health less frequently
connectionManager: {
maxPeers: 10,
Expand All @@ -36,12 +33,10 @@ export default function Libp2pBundle ({peerInfo, peerBook}) {
new DelegatedPeerRouter(delegatedApiOptions)
],
peerDiscovery: [
wrtcstar.discovery,
wsstar.discovery
wrtcstar.discovery
],
transport: [
wrtcstar,
wsstar,
Websockets
],
streamMuxer: [
Expand Down

0 comments on commit 0f13e53

Please sign in to comment.