From 7ef1c0a5989665f44806bd263293f252a797c20f Mon Sep 17 00:00:00 2001 From: David Dias Date: Sun, 9 Jul 2017 16:52:37 +0100 Subject: [PATCH] docs(examples): libp2p in the browser 1 --- .../1}/.gitignore | 0 .../1}/package.json | 21 +++++--- .../1}/public/index.html | 4 +- .../1/src/browser-bundle.js | 53 +++++++++++++++++++ .../1}/src/create-node.js | 4 +- .../1}/src/index.js | 0 examples/libp2p-in-the-browser/README.md | 16 +++++- examples/see-nodes/README.md | 12 ----- examples/see-nodes/src/libp2p-bundle.js | 27 ---------- 9 files changed, 85 insertions(+), 52 deletions(-) rename examples/{see-nodes => libp2p-in-the-browser/1}/.gitignore (100%) rename examples/{see-nodes => libp2p-in-the-browser/1}/package.json (55%) rename examples/{see-nodes => libp2p-in-the-browser/1}/public/index.html (72%) create mode 100644 examples/libp2p-in-the-browser/1/src/browser-bundle.js rename examples/{see-nodes => libp2p-in-the-browser/1}/src/create-node.js (80%) rename examples/{see-nodes => libp2p-in-the-browser/1}/src/index.js (100%) delete mode 100644 examples/see-nodes/README.md delete mode 100644 examples/see-nodes/src/libp2p-bundle.js diff --git a/examples/see-nodes/.gitignore b/examples/libp2p-in-the-browser/1/.gitignore similarity index 100% rename from examples/see-nodes/.gitignore rename to examples/libp2p-in-the-browser/1/.gitignore diff --git a/examples/see-nodes/package.json b/examples/libp2p-in-the-browser/1/package.json similarity index 55% rename from examples/see-nodes/package.json rename to examples/libp2p-in-the-browser/1/package.json index ae7648a2..84752f5b 100644 --- a/examples/see-nodes/package.json +++ b/examples/libp2p-in-the-browser/1/package.json @@ -1,22 +1,29 @@ { - "name": "see-nodes", - "version": "0.0.0", + "name": "libp2p-in-the-browser", + "version": "0.1.0", "description": "See other nodes in the network using WebRTC Star discovery mechanism", "main": "src/index.js", "scripts": { "bundle": "browserify src/index.js > public/bundle.js", "serve": "static public -p 9090 -H '{\"Cache-Control\": \"no-cache, must-revalidate\"}'", - "mon": "nodemon --exec \"npm run start\" --ignore public/bundle.js", "start": "npm run bundle && npm run serve" }, "license": "MIT", "devDependencies": { "browserify": "^14.0.0", - "browserify-optional": "^1.0.0", "concat-stream": "^1.6.0", "detect-dom-ready": "^1.0.2", - "node-static": "^0.7.9", - "nodemon": "^1.11.0" + "node-static": "^0.7.9" }, - "dependencies": {} + "dependencies": { + "detect-dom-ready": "^1.0.2", + "libp2p": "^0.10.0", + "libp2p-multiplex": "^0.4.4", + "libp2p-railing": "^0.5.2", + "libp2p-secio": "^0.6.8", + "libp2p-spdy": "^0.10.6", + "libp2p-webrtc-star": "^0.11.0", + "libp2p-websockets": "^0.10.0", + "peer-info": "^0.9.3" + } } diff --git a/examples/see-nodes/public/index.html b/examples/libp2p-in-the-browser/1/public/index.html similarity index 72% rename from examples/see-nodes/public/index.html rename to examples/libp2p-in-the-browser/1/public/index.html index 7d77a39c..c13adfe9 100644 --- a/examples/see-nodes/public/index.html +++ b/examples/libp2p-in-the-browser/1/public/index.html @@ -2,10 +2,10 @@ - p2p mapper + libp2p in the browser -

p2p mapper

+

libp2p node running \o/

diff --git a/examples/libp2p-in-the-browser/1/src/browser-bundle.js b/examples/libp2p-in-the-browser/1/src/browser-bundle.js new file mode 100644 index 00000000..11c78da7 --- /dev/null +++ b/examples/libp2p-in-the-browser/1/src/browser-bundle.js @@ -0,0 +1,53 @@ +'use strict' + +const WebRTCStar = require('libp2p-webrtc-star') +const WebSockets = require('libp2p-websockets') + +const Multiplex = require('libp2p-multiplex') +const SPDY = require('libp2p-spdy') +const SECIO = require('libp2p-secio') + +const Railing = require('libp2p-railing') +const libp2p = require('libp2p') + +// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json +const bootstrapers = [ + '/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', + '/dns4/sfo-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx', + '/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', + '/dns4/sfo-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z', + '/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', + '/dns4/sgp-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', + '/dns4/nyc-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', + '/dns4/nyc-2.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64' +] + +class Node extends libp2p { + constructor (peerInfo, peerBook, options) { + options = options || {} + + const wstar = new WebRTCStar() + + const modules = { + transport: [ + wstar, + new WebSockets() + ], + connection: { + muxer: [ + Multiplex, + SPDY + ], + crypto: [SECIO] + }, + discovery: [ + wstar.discovery, + new Railing(bootstrapers) + ] + } + + super(modules, peerInfo, peerBook, options) + } +} + +module.exports = Node diff --git a/examples/see-nodes/src/create-node.js b/examples/libp2p-in-the-browser/1/src/create-node.js similarity index 80% rename from examples/see-nodes/src/create-node.js rename to examples/libp2p-in-the-browser/1/src/create-node.js index 764363f8..70a012cf 100644 --- a/examples/see-nodes/src/create-node.js +++ b/examples/libp2p-in-the-browser/1/src/create-node.js @@ -1,7 +1,7 @@ 'use strict' const PeerInfo = require('peer-info') -const Node = require('./libp2p-bundle') +const Node = require('./browser-bundle') function createNode (callback) { PeerInfo.create((err, peerInfo) => { @@ -14,7 +14,7 @@ function createNode (callback) { peerInfo.multiaddrs.add(ma) - const node = new Node(peerInfo, undefined, { webRTCStar: true }) + const node = new Node(peerInfo) node.idStr = peerIdStr callback(null, node) diff --git a/examples/see-nodes/src/index.js b/examples/libp2p-in-the-browser/1/src/index.js similarity index 100% rename from examples/see-nodes/src/index.js rename to examples/libp2p-in-the-browser/1/src/index.js diff --git a/examples/libp2p-in-the-browser/README.md b/examples/libp2p-in-the-browser/README.md index 38f33cef..7a624a91 100644 --- a/examples/libp2p-in-the-browser/README.md +++ b/examples/libp2p-in-the-browser/README.md @@ -1,2 +1,14 @@ -# WIP - This example is still in the works -![](http://1.bp.blogspot.com/-tNvSnCW0KlQ/U-KOKGVoJkI/AAAAAAAAA3Q/aiSLMeSJFtw/s1600/WIP-sign.jpg) +# libp2p running in the Browser + +One of the primary goals with libp2p P2P was to get it fully working in the browser and interopable with the versions running in Go and in Node.js. + +# 1. Setting up a simple app that lists connections to other nodes + +Simple go into the folder [1](./1) and execute the following + +```bash +> cd 1 +> npm install +> npm start +# open your browser in port :9090 +``` diff --git a/examples/see-nodes/README.md b/examples/see-nodes/README.md deleted file mode 100644 index cc60cf79..00000000 --- a/examples/see-nodes/README.md +++ /dev/null @@ -1,12 +0,0 @@ -# See nodes - -> See other nodes in the network using WebRTC Star Discovery - -# Try it out - -``` -# After having run `npm install` at the root of the repo. -> npm install -> npm start -# open your browser in port :9090 -``` diff --git a/examples/see-nodes/src/libp2p-bundle.js b/examples/see-nodes/src/libp2p-bundle.js deleted file mode 100644 index 9a33e9ea..00000000 --- a/examples/see-nodes/src/libp2p-bundle.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict' - -const WebRTCStar = require('libp2p-webrtc-star') -const multiplex = require('libp2p-multiplex') -const spdy = require('libp2p-spdy') -const secio = require('libp2p-secio') -const libp2p = require('../../..') - -class Node extends libp2p { - constructor (peerInfo, peerBook, options) { - options = options || {} - const wstar = new WebRTCStar() - - const modules = { - transport: [wstar], - connection: { - muxer: [multiplex, spdy], - crypto: [secio] - }, - discovery: [wstar.discovery] - } - - super(modules, peerInfo, peerBook, options) - } -} - -module.exports = Node