Skip to content

Commit

Permalink
docs: improve browser example connectability
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Aug 20, 2018
1 parent fdd714e commit aab15ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
3 changes: 2 additions & 1 deletion examples/libp2p-in-the-browser/1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
},
"dependencies": {
"detect-dom-ready": "^1.0.2",
"libp2p-bootstrap": "~0.9.3",
"libp2p-mplex": "~0.8.0",
"libp2p-railing": "~0.9.1",
"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",
"peer-info": "~0.14.1"
}
Expand Down
14 changes: 0 additions & 14 deletions examples/libp2p-in-the-browser/1/public/index.html

This file was deleted.

17 changes: 12 additions & 5 deletions examples/libp2p-in-the-browser/1/src/browser-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

const WebRTCStar = require('libp2p-webrtc-star')
const WebSockets = require('libp2p-websockets')
const WebSocketStar = require('libp2p-websocket-star')
const Mplex = require('libp2p-mplex')
const SPDY = require('libp2p-spdy')
const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-railing')
const Bootstrap = require('libp2p-bootstrap')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../../../')

Expand All @@ -26,12 +27,14 @@ const bootstrapers = [
class Node extends libp2p {
constructor (_options) {
const wrtcStar = new WebRTCStar({ id: _options.peerInfo.id })
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })

const defaults = {
modules: {
transport: [
wrtcStar,
new WebSockets()
WebSockets,
wsstar
],
streamMuxer: [
Mplex,
Expand All @@ -42,6 +45,7 @@ class Node extends libp2p {
],
peerDiscovery: [
wrtcStar.discovery,
wsstar.discovery,
Bootstrap
]
},
Expand All @@ -55,21 +59,24 @@ class Node extends libp2p {
},
bootstrap: {
interval: 10000,
enabled: false,
enabled: true,
list: bootstrapers
}
},
relay: {
enabled: false,
enabled: true,
hop: {
enabled: false,
enabled: true,
active: false
}
},
EXPERIMENTAL: {
dht: false,
pubsub: false
}
},
connectionManager: {
maxPeers: 50
}
}

Expand Down
19 changes: 18 additions & 1 deletion examples/libp2p-in-the-browser/1/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint no-console: ["error", { allow: ["log"] }] */
/* eslint max-nested-callbacks: ["error", 5] */

'use strict'

const domReady = require('detect-dom-ready')
Expand All @@ -12,13 +15,27 @@ domReady(() => {
return console.log('Could not create the Node, check if your browser has WebRTC Support', err)
}

let connections = {}

node.on('peer:discovery', (peerInfo) => {
console.log('Discovered a peer')
const idStr = peerInfo.id.toB58String()
console.log('Discovered: ' + idStr)
if (connections[idStr]) {
// If we're already trying to connect to this peer, dont dial again
return
}

connections[idStr] = true
node.dial(peerInfo, (err, conn) => {
if (err) { return console.log('Failed to dial:', idStr) }
let timeToNextDial = 0
if (err) {
// Prevent immediate connection retries from happening
timeToNextDial = 30 * 1000
console.log('Failed to dial:', idStr)
}

setTimeout(() => delete connections[idStr], timeToNextDial)
})
})

Expand Down

0 comments on commit aab15ab

Please sign in to comment.