mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-16 15:40:49 +00:00
docs: refactor examples (#96)
This commit is contained in:
parent
606fa737b8
commit
547c4dbd33
@ -6,8 +6,6 @@ Let us know if you find any issue or if you want to contribute and add a new tut
|
||||
|
||||
## Examples
|
||||
|
||||
- [In Node.js](./nodejs)
|
||||
- [echo](./nodejs/echo)
|
||||
- [chat](./nodejs/chat)
|
||||
- [In the browser](./browser)
|
||||
- [mapper](./browser/mapper)
|
||||
- [The standard echo net example with libp2p](./echo)
|
||||
- [A simple chat app with](./chat)
|
||||
- [See other nodes in the network using WebRTC Star discovery mechanism](./see-nodes)
|
||||
|
1
examples/chat/README.md
Normal file
1
examples/chat/README.md
Normal file
@ -0,0 +1 @@
|
||||
# Chat example with libp2p
|
@ -3,8 +3,7 @@
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const async = require('async')
|
||||
const Pushable = require('pull-pushable')
|
||||
@ -31,12 +30,12 @@ async.parallel([
|
||||
], (err, ids) => {
|
||||
if (err) throw err
|
||||
const peerDialer = new PeerInfo(ids[0])
|
||||
peerDialer.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
|
||||
peerDialer.multiaddr.add('/ip4/0.0.0.0/tcp/0')
|
||||
const nodeDialer = new Node(peerDialer)
|
||||
|
||||
const peerListener = new PeerInfo(ids[1])
|
||||
idListener = ids[1]
|
||||
peerListener.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/10333'))
|
||||
peerListener.multiaddr.add('/ip4/127.0.0.1/tcp/10333')
|
||||
nodeDialer.start((err) => {
|
||||
if (err) {
|
||||
throw err
|
80
examples/chat/src/libp2p-bundle.js
Normal file
80
examples/chat/src/libp2p-bundle.js
Normal file
@ -0,0 +1,80 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Railing = require('libp2p-railing')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const multiplex = require('libp2p-multiplex')
|
||||
const secio = require('libp2p-secio')
|
||||
const libp2p = require('../..')
|
||||
|
||||
function mapMuxers (list) {
|
||||
return list.map((pref) => {
|
||||
if (typeof pref !== 'string') {
|
||||
return pref
|
||||
}
|
||||
switch (pref.trim().toLowerCase()) {
|
||||
case 'spdy': return spdy
|
||||
case 'multiplex': return multiplex
|
||||
default:
|
||||
throw new Error(pref + ' muxer not available')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getMuxers (muxers) {
|
||||
const muxerPrefs = process.env.LIBP2P_MUXER
|
||||
if (muxerPrefs && !muxers) {
|
||||
return mapMuxers(muxerPrefs.split(','))
|
||||
} else if (muxers) {
|
||||
return mapMuxers(muxers)
|
||||
} else {
|
||||
return [multiplex, spdy]
|
||||
}
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (peerInfo, peerBook, options) {
|
||||
options = options || {}
|
||||
|
||||
const modules = {
|
||||
transport: [
|
||||
new TCP(),
|
||||
new WS()
|
||||
],
|
||||
connection: {
|
||||
muxer: getMuxers(options.muxer),
|
||||
crypto: [ secio ]
|
||||
},
|
||||
discovery: []
|
||||
}
|
||||
|
||||
if (options.dht) {
|
||||
modules.DHT = KadDHT
|
||||
}
|
||||
|
||||
if (options.mdns) {
|
||||
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
|
||||
modules.discovery.push(mdns)
|
||||
}
|
||||
|
||||
if (options.bootstrap) {
|
||||
const r = new Railing(options.bootstrap)
|
||||
modules.discovery.push(r)
|
||||
}
|
||||
|
||||
if (options.modules && options.modules.transport) {
|
||||
options.modules.transport.forEach((t) => modules.transport.push(t))
|
||||
}
|
||||
|
||||
if (options.modules && options.modules.discovery) {
|
||||
options.modules.discovery.forEach((d) => modules.discovery.push(d))
|
||||
}
|
||||
|
||||
super(modules, peerInfo, peerBook, options)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
@ -3,8 +3,7 @@
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Node = require('./libp2p-bundle.js')
|
||||
const pull = require('pull-stream')
|
||||
const Pushable = require('pull-pushable')
|
||||
const p = Pushable()
|
||||
@ -14,7 +13,7 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
|
||||
throw err
|
||||
}
|
||||
const peerListener = new PeerInfo(idListener)
|
||||
peerListener.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/10333'))
|
||||
peerListener.multiaddr.add('/ip4/0.0.0.0/tcp/10333')
|
||||
const nodeListener = new Node(peerListener)
|
||||
|
||||
nodeListener.start((err) => {
|
1
examples/echo/README.md
Normal file
1
examples/echo/README.md
Normal file
@ -0,0 +1 @@
|
||||
# Echo example with libp2p
|
@ -7,8 +7,7 @@
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const async = require('async')
|
||||
|
||||
@ -21,14 +20,14 @@ async.parallel([
|
||||
// Dialer
|
||||
const dialerId = ids[0]
|
||||
const dialerPeerInfo = new PeerInfo(dialerId)
|
||||
dialerPeerInfo.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
|
||||
dialerPeerInfo.multiaddr.add('/ip4/0.0.0.0/tcp/0')
|
||||
const dialerNode = new Node(dialerPeerInfo)
|
||||
|
||||
// Peer to Dial
|
||||
const listenerPeerInfo = new PeerInfo(ids[1])
|
||||
const listenerId = ids[1]
|
||||
const listenerMultiaddr = multiaddr('/ip4/127.0.0.1/tcp/10333/ipfs/' +
|
||||
listenerId.toB58String())
|
||||
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/ipfs/' +
|
||||
listenerId.toB58String()
|
||||
listenerPeerInfo.multiaddr.add(listenerMultiaddr)
|
||||
|
||||
dialerNode.start((err) => {
|
80
examples/echo/src/libp2p-bundle.js
Normal file
80
examples/echo/src/libp2p-bundle.js
Normal file
@ -0,0 +1,80 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Railing = require('libp2p-railing')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const multiplex = require('libp2p-multiplex')
|
||||
const secio = require('libp2p-secio')
|
||||
const libp2p = require('../../..')
|
||||
|
||||
function mapMuxers (list) {
|
||||
return list.map((pref) => {
|
||||
if (typeof pref !== 'string') {
|
||||
return pref
|
||||
}
|
||||
switch (pref.trim().toLowerCase()) {
|
||||
case 'spdy': return spdy
|
||||
case 'multiplex': return multiplex
|
||||
default:
|
||||
throw new Error(pref + ' muxer not available')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getMuxers (muxers) {
|
||||
const muxerPrefs = process.env.LIBP2P_MUXER
|
||||
if (muxerPrefs && !muxers) {
|
||||
return mapMuxers(muxerPrefs.split(','))
|
||||
} else if (muxers) {
|
||||
return mapMuxers(muxers)
|
||||
} else {
|
||||
return [multiplex, spdy]
|
||||
}
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (peerInfo, peerBook, options) {
|
||||
options = options || {}
|
||||
|
||||
const modules = {
|
||||
transport: [
|
||||
new TCP(),
|
||||
new WS()
|
||||
],
|
||||
connection: {
|
||||
muxer: getMuxers(options.muxer),
|
||||
crypto: [ secio ]
|
||||
},
|
||||
discovery: []
|
||||
}
|
||||
|
||||
if (options.dht) {
|
||||
modules.DHT = KadDHT
|
||||
}
|
||||
|
||||
if (options.mdns) {
|
||||
const mdns = new MulticastDNS(peerInfo, 'ipfs.local')
|
||||
modules.discovery.push(mdns)
|
||||
}
|
||||
|
||||
if (options.bootstrap) {
|
||||
const r = new Railing(options.bootstrap)
|
||||
modules.discovery.push(r)
|
||||
}
|
||||
|
||||
if (options.modules && options.modules.transport) {
|
||||
options.modules.transport.forEach((t) => modules.transport.push(t))
|
||||
}
|
||||
|
||||
if (options.modules && options.modules.discovery) {
|
||||
options.modules.discovery.forEach((d) => modules.discovery.push(d))
|
||||
}
|
||||
|
||||
super(modules, peerInfo, peerBook, options)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
@ -7,8 +7,7 @@
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('../../../../test/nodejs-bundle/nodejs-bundle.js')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const series = require('async/series')
|
||||
|
||||
@ -25,7 +24,7 @@ series([
|
||||
},
|
||||
(cb) => {
|
||||
const listenerPeerInfo = new PeerInfo(listenerId)
|
||||
listenerPeerInfo.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/10333'))
|
||||
listenerPeerInfo.multiaddr.add('/ip4/0.0.0.0/tcp/10333')
|
||||
listenerNode = new Node(listenerPeerInfo)
|
||||
|
||||
listenerNode.swarm.on('peer-mux-established', (peerInfo) => {
|
||||
@ -33,7 +32,6 @@ series([
|
||||
})
|
||||
|
||||
listenerNode.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
|
||||
listenerNode.start(cb)
|
||||
}
|
||||
], (err) => {
|
12
examples/see-nodes/README.md
Normal file
12
examples/see-nodes/README.md
Normal file
@ -0,0 +1,12 @@
|
||||
# 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
|
||||
```
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "mapper",
|
||||
"name": "see-nodes",
|
||||
"version": "0.0.0",
|
||||
"description": "",
|
||||
"description": "See other nodes in the network using WebRTC Star discovery mechanism",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"bundle": "browserify src/index.js --require browserify-zlib-next:zlib > public/bundle.js",
|
||||
"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"
|
||||
@ -13,7 +13,6 @@
|
||||
"devDependencies": {
|
||||
"browserify": "^14.0.0",
|
||||
"browserify-optional": "^1.0.0",
|
||||
"browserify-zlib-next": "^1.0.1",
|
||||
"concat-stream": "^1.6.0",
|
||||
"detect-dom-ready": "^1.0.2",
|
||||
"node-static": "^0.7.9",
|
@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('../../../../test/browser-bundle/browser-bundle.js')
|
||||
const Node = require('./libp2p-bundle')
|
||||
|
||||
function createNode (callback) {
|
||||
PeerInfo.create((err, peerInfo) => {
|
27
examples/see-nodes/src/libp2p-bundle.js
Normal file
27
examples/see-nodes/src/libp2p-bundle.js
Normal file
@ -0,0 +1,27 @@
|
||||
'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
|
Loading…
x
Reference in New Issue
Block a user