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: examples #99

Merged
merged 8 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [Bundles](#bundles)
- [Usage](#usage)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Development](#development)
- [Tests](#tests)
Expand All @@ -46,7 +47,7 @@ We are in the process of writting better documentation, blog posts, tutorials an
- [libp2p.io](https://libp2p.io)
- [Specification (WIP)](https://github.com/libp2p/specs)
- Talks
- [`libp2p <3 ethereum` at DEVCON2](https://ethereumfoundation.org/devcon/?session=libp2p) [video](https://www.youtube.com/watch?v=HxueJbeMVG4) [slides](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p-HEART-devp2p-IPFS-PLUS-Ethereum-networking.pdf) [demo-1](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo1-1.mp4) [demo-2](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo2-1.mp4)
- [`libp2p <3 ethereum` at DEVCON2](https://ethereumfoundation.org/devcon/?session=libp2p) [📼 video](https://www.youtube.com/watch?v=HxueJbeMVG4) [slides](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p-HEART-devp2p-IPFS-PLUS-Ethereum-networking.pdf) [📼 demo-1](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo1-1.mp4) [📼 demo-2](https://ethereumfoundation.org/devcon/wp-content/uploads/2016/10/libp2p_demo2-1.mp4)
- Articles
- [The overview of libp2p](https://github.com/libp2p/libp2p#description)

Expand All @@ -71,6 +72,10 @@ npm install --save libp2p

## Usage

### [Tutorials and Examples](/examples)

You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarions.

### Extending libp2p skeleton

libp2p becomes very simple and basically acts as a glue for every module that compose this library. Since it can be highly customized, it requires some setup. What we recommend is to have a libp2p build for the system you are developing taking into account in your needs (e.g. for a browser working version of libp2p that acts as the network layer of IPFS, we have a built and minified version that browsers can require).
Expand Down
17 changes: 16 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ In this folder, you can find a variety of examples to help you get started in us

Let us know if you find any issue or if you want to contribute and add a new tutorial, feel welcome to submit a PR, thank you!

## Examples
## Understanding how libp2p works

- [Transports](./transports)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transports example is now ready for review 🌟

- [Protocol and Stream Muxing](./protocol-and-stream-muxing)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protocol and Stream Muxing is also ready for review 🌟

- [Encrypted Communications](./encrypted-communications)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one bytes the dust! 🌟

- [Discovery Mechanisms](./discovery-mechanisms)
- [Peer Routing](./peer-routing)
- [Content Routing](./content-routing)
- [PubSub](./pubsub)
- [NAT Traversal](./nat-traversal)
- Circuit Relay (future)
- Naming (future)

## Other examples

- [Running libp2p in the Browser](./libp2p-in-the-browser)
- Running libp2p in the Electron (future)
- [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)
2 changes: 2 additions & 0 deletions examples/discovery-mechanisms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# WIP - This example is still in the works
![](http://1.bp.blogspot.com/-tNvSnCW0KlQ/U-KOKGVoJkI/AAAAAAAAA3Q/aiSLMeSJFtw/s1600/WIP-sign.jpg)
2 changes: 2 additions & 0 deletions examples/encrypted-communications/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# WIP - This example is still in the works
![](http://1.bp.blogspot.com/-tNvSnCW0KlQ/U-KOKGVoJkI/AAAAAAAAA3Q/aiSLMeSJFtw/s1600/WIP-sign.jpg)
2 changes: 2 additions & 0 deletions examples/libp2p-in-the-browser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# WIP - This example is still in the works
![](http://1.bp.blogspot.com/-tNvSnCW0KlQ/U-KOKGVoJkI/AAAAAAAAA3Q/aiSLMeSJFtw/s1600/WIP-sign.jpg)
2 changes: 2 additions & 0 deletions examples/nat-traversal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# WIP - This example is still in the works
![](http://1.bp.blogspot.com/-tNvSnCW0KlQ/U-KOKGVoJkI/AAAAAAAAA3Q/aiSLMeSJFtw/s1600/WIP-sign.jpg)
2 changes: 2 additions & 0 deletions examples/peer-routing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# WIP - This example is still in the works
![](http://1.bp.blogspot.com/-tNvSnCW0KlQ/U-KOKGVoJkI/AAAAAAAAA3Q/aiSLMeSJFtw/s1600/WIP-sign.jpg)
96 changes: 96 additions & 0 deletions examples/protocol-and-stream-muxing/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict'

const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const pull = require('pull-stream')

class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()]
}
super(modules, peerInfo)
}
}

function createNode (callback) {
let node

waterfall([
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node.start(cb)
}
], (err) => callback(err, node))
}

parallel([
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, nodes) => {
if (err) { throw err }

const node1 = nodes[0]
const node2 = nodes[1]

// exact matching
node2.handle('/your-protocol', (protocol, conn) => {
pull(
conn,
pull.map((v) => v.toString()),
pull.log()
)
})

// semver matching
/*
node2.handle('/another-protocol/1.0.1', (protocol, conn) => {
pull(
conn,
pull.map((v) => v.toString()),
pull.log()
)
})
*/

// custom func matching
/*
node2.handle('/custom-match-func', (protocol, conn) => {
pull(
conn,
pull.map((v) => v.toString()),
pull.log()
)
}, (myProtocol, requestedProtocol, callback) => {
if (myProtocol.indexOf(requestedProtocol)) {
callback(null, true)
} else {
callback(null, false)
}
})
*/

node1.dial(node2.peerInfo, '/your-protocol', (err, conn) => {
if (err) { throw err }
pull(pull.values(['my own protocol, wow!']), conn)
})

/*
node1.dial(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
if (err) { throw err }
pull(pull.values(['semver me please']), conn)
})
*/

/*
node1.dial(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
if (err) { throw err }
pull(pull.values(['do I fall into your criteria?']), conn)
})
*/
})
79 changes: 79 additions & 0 deletions examples/protocol-and-stream-muxing/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'

const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
const pull = require('pull-stream')

class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [SPDY]
}
}
super(modules, peerInfo)
}
}

function createNode (callback) {
let node

waterfall([
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node.start(cb)
}
], (err) => callback(err, node))
}

parallel([
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, nodes) => {
if (err) { throw err }

const node1 = nodes[0]
const node2 = nodes[1]

node2.handle('/a', (protocol, conn) => {
pull(
conn,
pull.map((v) => v.toString()),
pull.log()
)
})

node2.handle('/b', (protocol, conn) => {
pull(
conn,
pull.map((v) => v.toString()),
pull.log()
)
})

series([
(cb) => node1.dial(node2.peerInfo, '/a', (err, conn) => {
if (err) { throw err }
pull(pull.values(['protocol (a)']), conn)
cb()
}),
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
if (err) { throw err }
pull(pull.values(['protocol (b)']), conn)
cb()
}),
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
if (err) { throw err }
pull(pull.values(['another conn on protocol (b)']), conn)
cb()
})
])
})
83 changes: 83 additions & 0 deletions examples/protocol-and-stream-muxing/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict'

const libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const SPDY = require('libp2p-spdy')
const PeerInfo = require('peer-info')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
const pull = require('pull-stream')

class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [SPDY]
}
}
super(modules, peerInfo)
}
}

function createNode (callback) {
let node

waterfall([
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node.start(cb)
}
], (err) => callback(err, node))
}

parallel([
(cb) => createNode(cb),
(cb) => createNode(cb)
], (err, nodes) => {
if (err) { throw err }

const node1 = nodes[0]
const node2 = nodes[1]

node1.handle('/node-1', (protocol, conn) => {
pull(
conn,
pull.map((v) => v.toString()),
pull.log()
)
})

node2.handle('/node-2', (protocol, conn) => {
pull(
conn,
pull.map((v) => v.toString()),
pull.log()
)
})

series([
(cb) => node1.dial(node2.peerInfo, '/node-2', (err, conn) => {
if (err) { throw err }
pull(pull.values(['from 1 to 2']), conn)
cb()
}),
(cb) => node2.dial(node1.peerInfo, '/node-1', (err, conn) => {
if (err) { throw err }
pull(pull.values(['from 2 to 1']), conn)
cb()
})
], (err) => {
if (err) { throw err }
console.log('Addresses by which both peers are connected')
node1.peerBook
.getAllArray()
.forEach((peer) => console.log('node 1 to node 2:', peer.isConnected().toString()))
node2.peerBook
.getAllArray()
.forEach((peer) => console.log('node 2 to node 1:', peer.isConnected().toString()))
})
})
Loading