mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-03 14:32:18 +00:00
refactor: examples-delegated-routing
This commit is contained in:
parent
51da8874d8
commit
f596ecdaa5
@ -10,7 +10,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
|
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.
|
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
|
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.
|
various Peer Discovery modules and see the impact it has on your Peer count.
|
||||||
|
|
||||||
@ -23,7 +23,9 @@ various Peer Discovery modules and see the impact it has on your Peer count.
|
|||||||
2. Run the IPFS daemon: `ipfs daemon`
|
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`
|
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.
|
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.
|
6. In `./src/App.js` replace `BootstrapNode` with your nodes Websocket address from step 4.
|
||||||
7. Start this example:
|
7. Start this example:
|
||||||
|
|
||||||
|
@ -3,19 +3,18 @@
|
|||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ipfs": "~0.34.4",
|
"ipfs": "~0.40.0",
|
||||||
"libp2p": "github:libp2p/js-libp2p#master",
|
"libp2p": "../..",
|
||||||
"libp2p-delegated-content-routing": "~0.2.2",
|
"libp2p-delegated-content-routing": "~0.4.1",
|
||||||
"libp2p-delegated-peer-routing": "~0.2.2",
|
"libp2p-delegated-peer-routing": "~0.4.0",
|
||||||
"libp2p-kad-dht": "~0.14.12",
|
"libp2p-kad-dht": "~0.18.2",
|
||||||
"libp2p-mplex": "~0.8.5",
|
"libp2p-mplex": "~0.9.3",
|
||||||
"libp2p-secio": "~0.11.1",
|
"libp2p-secio": "~0.12.1",
|
||||||
"libp2p-webrtc-star": "~0.15.8",
|
"libp2p-webrtc-star": "~0.17.0",
|
||||||
"libp2p-websocket-star": "~0.10.2",
|
"libp2p-websockets": "~0.13.1",
|
||||||
"libp2p-websockets": "~0.12.2",
|
"react": "^16.12.0",
|
||||||
"react": "^16.8.6",
|
"react-dom": "^16.12.0",
|
||||||
"react-dom": "^16.8.6",
|
"react-scripts": "3.3.0"
|
||||||
"react-scripts": "2.1.8"
|
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start"
|
"start": "react-scripts start"
|
||||||
|
@ -3,12 +3,11 @@
|
|||||||
|
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import Ipfs from 'ipfs'
|
import Ipfs from 'ipfs'
|
||||||
import libp2pBundle from './libp2p-bundle'
|
import libp2pConfig from './libp2p-configuration'
|
||||||
const Component = React.Component
|
|
||||||
|
|
||||||
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) {
|
constructor (props) {
|
||||||
super(props)
|
super(props)
|
||||||
this.state = {
|
this.state = {
|
||||||
@ -38,35 +37,31 @@ class App extends Component {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
handleHashSubmit (event) {
|
async handleHashSubmit (event) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
this.setState({
|
this.setState({
|
||||||
isLoading: this.state.isLoading + 1
|
isLoading: this.state.isLoading + 1
|
||||||
})
|
})
|
||||||
|
|
||||||
this.ipfs.cat(this.state.hash, (err, data) => {
|
const data = await this.ipfs.cat(this.state.hash)
|
||||||
if (err) console.log('Error', err)
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
response: data.toString(),
|
response: data.toString(),
|
||||||
isLoading: this.state.isLoading - 1
|
isLoading: this.state.isLoading - 1
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
handlePeerSubmit (event) {
|
async handlePeerSubmit (event) {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
this.setState({
|
this.setState({
|
||||||
isLoading: this.state.isLoading + 1
|
isLoading: this.state.isLoading + 1
|
||||||
})
|
})
|
||||||
|
|
||||||
this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
|
const results = await this.ipfs.dht.findpeer(this.state.peer)
|
||||||
if (err) console.log('Error', err)
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
response: JSON.stringify(results, null, 2),
|
response: JSON.stringify(results, null, 2),
|
||||||
isLoading: this.state.isLoading - 1
|
isLoading: this.state.isLoading - 1
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
@ -90,25 +85,22 @@ class App extends Component {
|
|||||||
preload: {
|
preload: {
|
||||||
enabled: false
|
enabled: false
|
||||||
},
|
},
|
||||||
libp2p: libp2pBundle
|
libp2p: libp2pConfig
|
||||||
})
|
})
|
||||||
this.ipfs.on('ready', () => {
|
|
||||||
|
this.ipfs.on('ready', async () => {
|
||||||
if (this.peerInterval) {
|
if (this.peerInterval) {
|
||||||
clearInterval(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(() => {
|
await this.ipfs.swarm.connect(BootstrapNode)
|
||||||
this.ipfs.swarm.peers((err, peers) => {
|
console.log('Connected!')
|
||||||
if (err) console.log(err)
|
|
||||||
|
this.peerInterval = setInterval(async () => {
|
||||||
|
const peers = await this.ipfs.swarm.peers()
|
||||||
|
|
||||||
if (peers) this.setState({peers: peers.length})
|
if (peers) this.setState({peers: peers.length})
|
||||||
})
|
|
||||||
}, 2500)
|
}, 2500)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
const Libp2p = require('libp2p')
|
const Libp2p = require('libp2p')
|
||||||
const Websockets = require('libp2p-websockets')
|
const Websockets = require('libp2p-websockets')
|
||||||
const WebSocketStar = require('libp2p-websocket-star')
|
|
||||||
const WebRTCStar = require('libp2p-webrtc-star')
|
const WebRTCStar = require('libp2p-webrtc-star')
|
||||||
const MPLEX = require('libp2p-mplex')
|
const MPLEX = require('libp2p-mplex')
|
||||||
const SECIO = require('libp2p-secio')
|
const SECIO = require('libp2p-secio')
|
||||||
@ -11,18 +10,16 @@ const KadDHT = require('libp2p-kad-dht')
|
|||||||
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
|
||||||
const DelegatedContentRouter = require('libp2p-delegated-content-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 wrtcstar = new WebRTCStar({id: peerInfo.id})
|
||||||
const wsstar = new WebSocketStar({id: peerInfo.id})
|
|
||||||
const delegatedApiOptions = {
|
const delegatedApiOptions = {
|
||||||
host: '0.0.0.0',
|
host: '127.0.0.1',
|
||||||
protocol: 'http',
|
protocol: 'http',
|
||||||
port: '8080'
|
port: '5001'
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Libp2p({
|
return new Libp2p({
|
||||||
peerInfo,
|
peerInfo,
|
||||||
peerBook,
|
|
||||||
// Lets limit the connection managers peers and have it check peer health less frequently
|
// Lets limit the connection managers peers and have it check peer health less frequently
|
||||||
connectionManager: {
|
connectionManager: {
|
||||||
maxPeers: 10,
|
maxPeers: 10,
|
||||||
@ -36,12 +33,10 @@ export default function Libp2pBundle ({peerInfo, peerBook}) {
|
|||||||
new DelegatedPeerRouter(delegatedApiOptions)
|
new DelegatedPeerRouter(delegatedApiOptions)
|
||||||
],
|
],
|
||||||
peerDiscovery: [
|
peerDiscovery: [
|
||||||
wrtcstar.discovery,
|
wrtcstar.discovery
|
||||||
wsstar.discovery
|
|
||||||
],
|
],
|
||||||
transport: [
|
transport: [
|
||||||
wrtcstar,
|
wrtcstar,
|
||||||
wsstar,
|
|
||||||
Websockets
|
Websockets
|
||||||
],
|
],
|
||||||
streamMuxer: [
|
streamMuxer: [
|
Loading…
x
Reference in New Issue
Block a user