Skip to content

Commit

Permalink
docs: update examples (#2319)
Browse files Browse the repository at this point in the history
Since I was going through these anyway to ensure they work with [0.37](ipfs/js-ipfs#2192) I've updated the examples to use the new [`IPFS.create` constructor](https://github.com/ipfs/js-ipfs#ipfs-constructor) and switched to using the promised API so that we onboard new users to using promises and minimise the disruption caused when ipfs/js-ipfs#1670 bubbles up to here.

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Aug 2, 2019
1 parent 9d4fa53 commit 1f2e04b
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const MPLEX = require('pull-mplex')
const SECIO = require('libp2p-secio')
const assert = require('assert')

/**
* Options for the libp2p bundle
Expand Down Expand Up @@ -104,33 +103,31 @@ const libp2pBundle = (opts) => {
})
}

// Now that we have our custom libp2p bundle, let's start up the ipfs node!
const node = new IPFS({
libp2p: libp2pBundle
})

// Listen for the node to start, so we can log out some metrics
node.once('start', (err) => {
assert.ifError(err, 'Should startup without issue')
async function main () {
// Now that we have our custom libp2p bundle, let's start up the ipfs node!
const node = await IPFS.create({
libp2p: libp2pBundle
})

// Lets log out the number of peers we have every 2 seconds
setInterval(() => {
node.swarm.peers((err, peers) => {
if (err) {
console.log('An error occurred trying to check our peers:', err)
process.exit(1)
}
setInterval(async () => {
try {
const peers = await node.swarm.peers()
console.log(`The node now has ${peers.length} peers.`)
})
} catch (err) {
console.log('An error occurred trying to check our peers:', err)
}
}, 2000)

// Log out the bandwidth stats every 4 seconds so we can see how our configuration is doing
setInterval(() => {
node.stats.bw((err, stats) => {
if (err) {
console.log('An error occurred trying to check our stats:', err)
}
setInterval(async () => {
try {
const stats = await node.stats.bw()
console.log(`\nBandwidth Stats: ${JSON.stringify(stats, null, 2)}\n`)
})
} catch (err) {
console.log('An error occurred trying to check our stats:', err)
}
}, 4000)
})
}

main()

0 comments on commit 1f2e04b

Please sign in to comment.