Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve browser example connectability #240

Merged
merged 2 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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