From 4cb541ddaec81be308c4f22194230e11c0339c51 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Thu, 11 Apr 2019 15:52:04 +0200 Subject: [PATCH] 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 --- examples/chat/README.md | 4 +-- examples/delegated-routing/package.json | 30 +++++++++++-------- examples/delegated-routing/src/App.js | 9 +++--- examples/delegated-routing/src/index.js | 7 ++--- .../delegated-routing/src/libp2p-bundle.js | 8 ++--- examples/discovery-mechanisms/1.js | 5 ++-- examples/discovery-mechanisms/2.js | 2 +- examples/echo/README.md | 2 +- examples/libp2p-in-the-browser/1/package.json | 14 ++++----- .../1/src/browser-bundle.js | 14 ++++++--- examples/libp2p-in-the-browser/1/src/index.js | 24 ++------------- examples/peer-and-content-routing/1.js | 4 +-- examples/peer-and-content-routing/2.js | 4 +-- examples/peer-and-content-routing/README.md | 6 ++-- examples/pubsub/1.js | 7 ++--- examples/pubsub/README.md | 10 +++---- 16 files changed, 64 insertions(+), 86 deletions(-) diff --git a/examples/chat/README.md b/examples/chat/README.md index 23fe4c5d..87175dc7 100644 --- a/examples/chat/README.md +++ b/examples/chat/README.md @@ -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` diff --git a/examples/delegated-routing/package.json b/examples/delegated-routing/package.json index ff36b7bd..164e0d3c 100644 --- a/examples/delegated-routing/package.json +++ b/examples/delegated-routing/package.json @@ -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" + ] } diff --git a/examples/delegated-routing/src/App.js b/examples/delegated-routing/src/App.js index c3d18d6e..2547496b 100644 --- a/examples/delegated-routing/src/App.js +++ b/examples/delegated-routing/src/App.js @@ -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 diff --git a/examples/delegated-routing/src/index.js b/examples/delegated-routing/src/index.js index 366e8650..c41a66a3 100644 --- a/examples/delegated-routing/src/index.js +++ b/examples/delegated-routing/src/index.js @@ -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(, document.getElementById('root')) diff --git a/examples/delegated-routing/src/libp2p-bundle.js b/examples/delegated-routing/src/libp2p-bundle.js index c5ab0dc7..48fb0906 100644 --- a/examples/delegated-routing/src/libp2p-bundle.js +++ b/examples/delegated-routing/src/libp2p-bundle.js @@ -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 } } }) diff --git a/examples/discovery-mechanisms/1.js b/examples/discovery-mechanisms/1.js index eebd47b8..f2cd89de 100644 --- a/examples/discovery-mechanisms/1.js +++ b/examples/discovery-mechanisms/1.js @@ -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) => { diff --git a/examples/discovery-mechanisms/2.js b/examples/discovery-mechanisms/2.js index 1711b3f0..8c5c630e 100644 --- a/examples/discovery-mechanisms/2.js +++ b/examples/discovery-mechanisms/2.js @@ -23,7 +23,7 @@ class MyBundle extends libp2p { config: { peerDiscovery: { mdns: { - interval: 1000, + interval: 20e3, enabled: true } } diff --git a/examples/echo/README.md b/examples/echo/README.md index 01c672c9..495b83ab 100644 --- a/examples/echo/README.md +++ b/examples/echo/README.md @@ -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 diff --git a/examples/libp2p-in-the-browser/1/package.json b/examples/libp2p-in-the-browser/1/package.json index 03460419..67d6d4ff 100644 --- a/examples/libp2p-in-the-browser/1/package.json +++ b/examples/libp2p-in-the-browser/1/package.json @@ -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" } } diff --git a/examples/libp2p-in-the-browser/1/src/browser-bundle.js b/examples/libp2p-in-the-browser/1/src/browser-bundle.js index 60f5c2ae..798df886 100644 --- a/examples/libp2p-in-the-browser/1/src/browser-bundle.js +++ b/examples/libp2p-in-the-browser/1/src/browser-bundle.js @@ -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 } } diff --git a/examples/libp2p-in-the-browser/1/src/index.js b/examples/libp2p-in-the-browser/1/src/index.js index 3b2923f9..da3cea73 100644 --- a/examples/libp2p-in-the-browser/1/src/index.js +++ b/examples/libp2p-in-the-browser/1/src/index.js @@ -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() diff --git a/examples/peer-and-content-routing/1.js b/examples/peer-and-content-routing/1.js index d9a09df2..605ec307 100644 --- a/examples/peer-and-content-routing/1.js +++ b/examples/peer-and-content-routing/1.js @@ -23,10 +23,8 @@ class MyBundle extends libp2p { }, config: { dht: { + enabled: true, kBucketSize: 20 - }, - EXPERIMENTAL: { - dht: true } } } diff --git a/examples/peer-and-content-routing/2.js b/examples/peer-and-content-routing/2.js index f3f77d49..78b187c5 100644 --- a/examples/peer-and-content-routing/2.js +++ b/examples/peer-and-content-routing/2.js @@ -24,10 +24,8 @@ class MyBundle extends libp2p { }, config: { dht: { + enabled: true, kBucketSize: 20 - }, - EXPERIMENTAL: { - dht: true } } } diff --git a/examples/peer-and-content-routing/README.md b/examples/peer-and-content-routing/README.md index 7abf10cc..e00903ef 100644 --- a/examples/peer-and-content-routing/README.md +++ b/examples/peer-and-content-routing/README.md @@ -25,11 +25,9 @@ class MyBundle extends libp2p { }, config: { dht: { - kBucketSize: 20 - }, - EXPERIMENTAL: { // dht must be enabled - dht: true + enabled: true, + kBucketSize: 20 } } } diff --git a/examples/pubsub/1.js b/examples/pubsub/1.js index 8adfd35d..e80315a2 100644 --- a/examples/pubsub/1.js +++ b/examples/pubsub/1.js @@ -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', diff --git a/examples/pubsub/README.md b/examples/pubsub/README.md index a058c04d..7e5299f0 100644 --- a/examples/pubsub/README.md +++ b/examples/pubsub/README.md @@ -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!