1
0
mirror of https://github.com/fluencelabs/js-libp2p synced 2025-03-16 15:40:49 +00:00

docs: update examples for latest libp2p rc ()

* 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
examples
chat
delegated-routing
discovery-mechanisms
echo
libp2p-in-the-browser/1
peer-and-content-routing
pubsub

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

@ -3,21 +3,27 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"ipfs": "~0.32.2",
"libp2p": "../../",
"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.10.4",
"libp2p-mplex": "~0.8.0",
"libp2p-secio": "~0.10.0",
"libp2p-webrtc-star": "~0.15.5",
"libp2p-websocket-star": "~0.8.1",
"libp2p-websockets": "~0.12.0",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-scripts": "1.1.5"
"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"
},
"scripts": {
"start": "react-scripts start"
}
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}

@ -1,11 +1,10 @@
// eslint-disable-next-line
'use strict'
const React = require('react')
import React from 'react'
import Ipfs from 'ipfs'
import libp2pBundle from './libp2p-bundle'
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'
@ -150,4 +149,4 @@ class App extends Component {
}
}
module.exports = App
export default App

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

@ -11,7 +11,7 @@ const KadDHT = require('libp2p-kad-dht')
const DelegatedPeerRouter = require('libp2p-delegated-peer-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 wsstar = new WebSocketStar({id: peerInfo.id})
const delegatedApiOptions = {
@ -54,6 +54,7 @@ module.exports = ({peerInfo, peerBook}) => {
},
config: {
peerDiscovery: {
autoDial: false,
webrtcStar: {
enabled: false
},
@ -62,16 +63,13 @@ module.exports = ({peerInfo, peerBook}) => {
}
},
dht: {
kBucketSize: 20
enabled: false
},
relay: {
enabled: true,
hop: {
enabled: false
}
},
EXPERIMENTAL: {
dht: false
}
}
})

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

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

@ -3,7 +3,7 @@
This example performs a simple echo from the listener to the dialer.
## 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.
## Running

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

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

@ -14,26 +14,8 @@ domReady(() => {
return console.log('Could not create the Node, check if your browser has WebRTC Support', err)
}
let connections = {}
node.on('peer:discovery', (peerInfo) => {
const idStr = 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)
}
})
console.log('Discovered a peer:', peerInfo.id.toB58String())
})
node.on('peer:connect', (peerInfo) => {
@ -47,15 +29,13 @@ domReady(() => {
node.on('peer:disconnect', (peerInfo) => {
const idStr = peerInfo.id.toB58String()
delete connections[idStr]
console.log('Lost connection to: ' + idStr)
const el = document.getElementById(idStr)
el && el.remove()
})
node.start((err) => {
if (err) {
return console.log('WebRTC not supported')
return console.log(err)
}
const idStr = node.peerInfo.id.toB58String()

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

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

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

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

@ -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).
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
series([
(cb) => node1.once('peer:discovery', (peer) => node1.dial(peer, cb)),
(cb) => setTimeout(cb, 500)
], (err) => {
if (err) { throw err }
node1.once('peer:connect', (peer) => {
console.log('connected to %s', peer.id.toB58String())
// Subscribe to the topic 'news'
node1.pubsub.subscribe('news',
@ -42,6 +39,7 @@ The output of the program should look like:
```
> 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!