docs: update examples for latest libp2p rc (#353)

* docs: update chat example readme

* docs: update discovery test for autodial

* docs: fix delegated routing example

* docs: update echo example readme

* docs: fix libp2p in the browser example

* docs: update examples for peer/content routing

* docs: update the pubsub example
This commit is contained in:
Jacob Heun 2019-04-11 15:52:04 +02:00 committed by GitHub
parent aa1d9b273a
commit 4cb541ddae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 64 additions and 86 deletions

View File

@ -3,8 +3,8 @@
This example creates a simple chat app in your terminal. This example creates a simple chat app in your terminal.
## Setup ## Setup
1. Install the modules, `npm install`. 1. Install the modules in the libp2p root directory, `npm install`.
2. Open 2 terminal windows in the `./src` directory. 2. Open 2 terminal windows in the `./examples/chat/src` directory.
## Running ## Running
1. Run the listener in window 1, `node listener.js` 1. Run the listener in window 1, `node listener.js`

View File

@ -3,21 +3,27 @@
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"ipfs": "~0.32.2", "ipfs": "~0.34.4",
"libp2p": "../../", "libp2p": "github:libp2p/js-libp2p#master",
"libp2p-delegated-content-routing": "~0.2.2", "libp2p-delegated-content-routing": "~0.2.2",
"libp2p-delegated-peer-routing": "~0.2.2", "libp2p-delegated-peer-routing": "~0.2.2",
"libp2p-kad-dht": "~0.10.4", "libp2p-kad-dht": "~0.14.12",
"libp2p-mplex": "~0.8.0", "libp2p-mplex": "~0.8.5",
"libp2p-secio": "~0.10.0", "libp2p-secio": "~0.11.1",
"libp2p-webrtc-star": "~0.15.5", "libp2p-webrtc-star": "~0.15.8",
"libp2p-websocket-star": "~0.8.1", "libp2p-websocket-star": "~0.10.2",
"libp2p-websockets": "~0.12.0", "libp2p-websockets": "~0.12.2",
"react": "^16.5.2", "react": "^16.8.6",
"react-dom": "^16.5.2", "react-dom": "^16.8.6",
"react-scripts": "1.1.5" "react-scripts": "2.1.8"
}, },
"scripts": { "scripts": {
"start": "react-scripts start" "start": "react-scripts start"
} },
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
} }

View File

@ -1,11 +1,10 @@
// eslint-disable-next-line // eslint-disable-next-line
'use strict' 'use strict'
const React = require('react') import React from 'react'
import Ipfs from 'ipfs'
import libp2pBundle from './libp2p-bundle'
const Component = React.Component const Component = React.Component
const Ipfs = require('ipfs')
const libp2pBundle = require('./libp2p-bundle')
// require('./App.css')
const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/p2p/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8' const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/p2p/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
@ -150,4 +149,4 @@ class App extends Component {
} }
} }
module.exports = App export default App

View File

@ -1,9 +1,8 @@
// eslint-disable-next-line // eslint-disable-next-line
'use strict' 'use strict'
const React = require('react') // eslint-disable-line no-unused-vars import React from 'react' // eslint-disable-line no-unused-vars
const ReactDOM = require('react-dom') import ReactDOM from 'react-dom'
const App = require('./App') // eslint-disable-line no-unused-vars import App from './App' // eslint-disable-line no-unused-vars
// require('index.css')
ReactDOM.render(<App />, document.getElementById('root')) ReactDOM.render(<App />, document.getElementById('root'))

View File

@ -11,7 +11,7 @@ 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')
module.exports = ({peerInfo, peerBook}) => { export default function Libp2pBundle ({peerInfo, peerBook}) {
const wrtcstar = new WebRTCStar({id: peerInfo.id}) const wrtcstar = new WebRTCStar({id: peerInfo.id})
const wsstar = new WebSocketStar({id: peerInfo.id}) const wsstar = new WebSocketStar({id: peerInfo.id})
const delegatedApiOptions = { const delegatedApiOptions = {
@ -54,6 +54,7 @@ module.exports = ({peerInfo, peerBook}) => {
}, },
config: { config: {
peerDiscovery: { peerDiscovery: {
autoDial: false,
webrtcStar: { webrtcStar: {
enabled: false enabled: false
}, },
@ -62,16 +63,13 @@ module.exports = ({peerInfo, peerBook}) => {
} }
}, },
dht: { dht: {
kBucketSize: 20 enabled: false
}, },
relay: { relay: {
enabled: true, enabled: true,
hop: { hop: {
enabled: false enabled: false
} }
},
EXPERIMENTAL: {
dht: false
} }
} }
}) })

View File

@ -34,8 +34,9 @@ class MyBundle extends libp2p {
}, },
config: { config: {
peerDiscovery: { peerDiscovery: {
autoDial: true,
bootstrap: { bootstrap: {
interval: 2000, interval: 20e3,
enabled: true, enabled: true,
list: bootstrapers list: bootstrapers
} }
@ -62,8 +63,8 @@ waterfall([
if (err) { throw err } if (err) { throw err }
node.on('peer:discovery', (peer) => { node.on('peer:discovery', (peer) => {
// No need to dial, autoDial is on
console.log('Discovered:', peer.id.toB58String()) console.log('Discovered:', peer.id.toB58String())
node.dial(peer, () => {})
}) })
node.on('peer:connect', (peer) => { node.on('peer:connect', (peer) => {

View File

@ -23,7 +23,7 @@ class MyBundle extends libp2p {
config: { config: {
peerDiscovery: { peerDiscovery: {
mdns: { mdns: {
interval: 1000, interval: 20e3,
enabled: true enabled: true
} }
} }

View File

@ -3,7 +3,7 @@
This example performs a simple echo from the listener to the dialer. This example performs a simple echo from the listener to the dialer.
## Setup ## Setup
1. Install the modules, `npm install`. 1. Install the modules from libp2p root, `npm install`.
2. Open 2 terminal windows in the `./src` directory. 2. Open 2 terminal windows in the `./src` directory.
## Running ## Running

View File

@ -17,13 +17,13 @@
}, },
"dependencies": { "dependencies": {
"detect-dom-ready": "^1.0.2", "detect-dom-ready": "^1.0.2",
"libp2p-bootstrap": "~0.9.3", "libp2p-bootstrap": "~0.9.7",
"libp2p-mplex": "~0.8.0", "libp2p-mplex": "~0.8.5",
"libp2p-secio": "~0.10.0", "libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.12.1", "libp2p-spdy": "~0.13.3",
"libp2p-webrtc-star": "~0.15.3", "libp2p-webrtc-star": "~0.15.8",
"libp2p-websocket-star": "~0.8.1", "libp2p-websocket-star": "~0.10.2",
"libp2p-websockets": "~0.12.0", "libp2p-websockets": "~0.12.2",
"peer-info": "~0.15.1" "peer-info": "~0.15.1"
} }
} }

View File

@ -7,6 +7,7 @@ const Mplex = require('libp2p-mplex')
const SPDY = require('libp2p-spdy') const SPDY = require('libp2p-spdy')
const SECIO = require('libp2p-secio') const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap') const Bootstrap = require('libp2p-bootstrap')
const DHT = require('libp2p-kad-dht')
const defaultsDeep = require('@nodeutils/defaults-deep') const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../../../') const libp2p = require('../../../../')
@ -47,10 +48,12 @@ class Node extends libp2p {
wrtcStar.discovery, wrtcStar.discovery,
wsstar.discovery, wsstar.discovery,
Bootstrap Bootstrap
] ],
dht: DHT
}, },
config: { config: {
peerDiscovery: { peerDiscovery: {
autoDial: true,
webRTCStar: { webRTCStar: {
enabled: true enabled: true
}, },
@ -58,7 +61,7 @@ class Node extends libp2p {
enabled: true enabled: true
}, },
bootstrap: { bootstrap: {
interval: 10000, interval: 20e3,
enabled: true, enabled: true,
list: bootstrapList list: bootstrapList
} }
@ -66,16 +69,19 @@ class Node extends libp2p {
relay: { relay: {
enabled: true, enabled: true,
hop: { hop: {
enabled: true, enabled: false,
active: false active: false
} }
}, },
dht: {
enabled: false
},
EXPERIMENTAL: { EXPERIMENTAL: {
dht: false,
pubsub: false pubsub: false
} }
}, },
connectionManager: { connectionManager: {
minPeers: 10,
maxPeers: 50 maxPeers: 50
} }
} }

View File

@ -14,26 +14,8 @@ domReady(() => {
return console.log('Could not create the Node, check if your browser has WebRTC Support', err) return console.log('Could not create the Node, check if your browser has WebRTC Support', err)
} }
let connections = {}
node.on('peer:discovery', (peerInfo) => { node.on('peer:discovery', (peerInfo) => {
const idStr = peerInfo.id.toB58String() console.log('Discovered a peer:', peerInfo.id.toB58String())
if (connections[idStr]) {
// If we're already trying to connect to this peer, dont dial again
return
}
console.log('Discovered a peer:', idStr)
connections[idStr] = true
node.dial(peerInfo, (err, conn) => {
if (err) {
// Prevent immediate connection retries from happening
// and include a 10s jitter
const timeToNextDial = 25 * 1000 + (Math.random(0) * 10000).toFixed(0)
console.log('Failed to dial:', idStr)
setTimeout(() => delete connections[idStr], timeToNextDial)
}
})
}) })
node.on('peer:connect', (peerInfo) => { node.on('peer:connect', (peerInfo) => {
@ -47,15 +29,13 @@ domReady(() => {
node.on('peer:disconnect', (peerInfo) => { node.on('peer:disconnect', (peerInfo) => {
const idStr = peerInfo.id.toB58String() const idStr = peerInfo.id.toB58String()
delete connections[idStr]
console.log('Lost connection to: ' + idStr)
const el = document.getElementById(idStr) const el = document.getElementById(idStr)
el && el.remove() el && el.remove()
}) })
node.start((err) => { node.start((err) => {
if (err) { if (err) {
return console.log('WebRTC not supported') return console.log(err)
} }
const idStr = node.peerInfo.id.toB58String() const idStr = node.peerInfo.id.toB58String()

View File

@ -23,10 +23,8 @@ class MyBundle extends libp2p {
}, },
config: { config: {
dht: { dht: {
enabled: true,
kBucketSize: 20 kBucketSize: 20
},
EXPERIMENTAL: {
dht: true
} }
} }
} }

View File

@ -24,10 +24,8 @@ class MyBundle extends libp2p {
}, },
config: { config: {
dht: { dht: {
enabled: true,
kBucketSize: 20 kBucketSize: 20
},
EXPERIMENTAL: {
dht: true
} }
} }
} }

View File

@ -25,11 +25,9 @@ class MyBundle extends libp2p {
}, },
config: { config: {
dht: { dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
// dht must be enabled // dht must be enabled
dht: true enabled: true,
kBucketSize: 20
} }
} }
} }

View File

@ -62,11 +62,8 @@ parallel([
const node1 = nodes[0] const node1 = nodes[0]
const node2 = nodes[1] const node2 = nodes[1]
series([ node1.once('peer:connect', (peer) => {
(cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)), console.log('connected to %s', peer.id.toB58String())
(cb) => setTimeout(cb, 500)
], (err) => {
if (err) { throw err }
// Subscribe to the topic 'news' // Subscribe to the topic 'news'
node1.pubsub.subscribe('news', node1.pubsub.subscribe('news',

View File

@ -12,14 +12,11 @@ We've seen many interesting use cases appear with this, here are some highlights
For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js). For this example, we will use MulticastDNS for automatic Peer Discovery. This example is based the previous examples found in [Discovery Mechanisms](../discovery-mechanisms). You can find the complete version at [1.js](./1.js).
Using PubSub is super simple, all you have to do is start a libp2p node, PubSub will be enabled by default. Using PubSub is super simple, all you have to do is start a libp2p node with `EXPERIMENTAL.pubsub` set to true.
```JavaScript ```JavaScript
series([ node1.once('peer:connect', (peer) => {
(cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)), console.log('connected to %s', peer.id.toB58String())
(cb) => setTimeout(cb, 500)
], (err) => {
if (err) { throw err }
// Subscribe to the topic 'news' // Subscribe to the topic 'news'
node1.pubsub.subscribe('news', node1.pubsub.subscribe('news',
@ -42,6 +39,7 @@ The output of the program should look like:
``` ```
> node 1.js > node 1.js
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word! QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word! QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!
QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word! QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82 Bird bird bird, bird is the word!