-
Notifications
You must be signed in to change notification settings - Fork 445
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
docs: examples #99
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
53cce8d
docs: list examples
daviddias 32cb59f
docs(examples): transports 1
daviddias 2b02be0
docs(examples): transports 2
daviddias 7b78736
docs(examples): transports 3
daviddias b14a2ff
docs(examples): transports 4
daviddias 7df0074
docs: add disclaimers for each example
daviddias 5f5ee98
docs(examples): protocols and stream muxing 1, 2 and 3
daviddias f583b1b
docs(examples): encrypted communicationsls
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
- [Stream and Protocol Muxing](./protocol-and-stream-muxing) | ||
- [Encrypted Communications](./encrypted-communications) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const libp2p = require('libp2p') | ||
const TCP = require('libp2p-tcp') | ||
const PeerInfo = require('peer-info') | ||
const waterfall = require('async/waterfall') | ||
|
||
class MyBundle extends libp2p { | ||
constructor (peerInfo) { | ||
const modules = { | ||
transport: [new TCP()] | ||
} | ||
super(modules, peerInfo) | ||
} | ||
} | ||
|
||
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) => { | ||
if (err) { throw err } | ||
|
||
console.log('node has started (true/false):', node.isOn()) | ||
console.log('listening on:') | ||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString())) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'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)) | ||
} | ||
|
||
function printAddrs (node, number) { | ||
console.log('node %s is listening on:', number) | ||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString())) | ||
} | ||
|
||
parallel([ | ||
(cb) => createNode(cb), | ||
(cb) => createNode(cb) | ||
], (err, nodes) => { | ||
if (err) { throw err } | ||
|
||
const node1 = nodes[0] | ||
const node2 = nodes[1] | ||
|
||
printAddrs(node1, '1') | ||
printAddrs(node2, '2') | ||
|
||
node2.handle('/print', (protocol, conn) => { | ||
pull( | ||
conn, | ||
pull.map((v) => v.toString()), | ||
pull.log() | ||
) | ||
}) | ||
|
||
node1.dial(node2.peerInfo, '/print', (err, conn) => { | ||
if (err) { throw err } | ||
|
||
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict' | ||
|
||
const libp2p = require('libp2p') | ||
const TCP = require('libp2p-tcp') | ||
const WebSockets = require('libp2p-websockets') | ||
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(), new WebSockets()] | ||
} | ||
super(modules, peerInfo) | ||
} | ||
} | ||
|
||
function createNode (addrs, callback) { | ||
if (!Array.isArray(addrs)) { | ||
addrs = [addrs] | ||
} | ||
|
||
let node | ||
|
||
waterfall([ | ||
(cb) => PeerInfo.create(cb), | ||
(peerInfo, cb) => { | ||
addrs.forEach((addr) => peerInfo.multiaddrs.add(addr)) | ||
node = new MyBundle(peerInfo) | ||
node.start(cb) | ||
} | ||
], (err) => callback(err, node)) | ||
} | ||
|
||
function printAddrs (node, number) { | ||
console.log('node %s is listening on:', number) | ||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString())) | ||
} | ||
|
||
function print (protocol, conn) { | ||
pull( | ||
conn, | ||
pull.map((v) => v.toString()), | ||
pull.log() | ||
) | ||
} | ||
|
||
parallel([ | ||
(cb) => createNode('/ip4/0.0.0.0/tcp/0', cb), | ||
(cb) => createNode(['/ip4/0.0.0.0/tcp/0', '/ip4/127.0.0.1/tcp/10000/ws'], cb), | ||
(cb) => createNode('/ip4/127.0.0.1/tcp/20000/ws', cb) | ||
], (err, nodes) => { | ||
if (err) { throw err } | ||
|
||
const node1 = nodes[0] | ||
const node2 = nodes[1] | ||
const node3 = nodes[2] | ||
|
||
printAddrs(node1, '1') | ||
printAddrs(node2, '2') | ||
printAddrs(node3, '3') | ||
|
||
node1.handle('/print', print) | ||
node2.handle('/print', print) | ||
node3.handle('/print', print) | ||
|
||
node1.dial(node2.peerInfo, '/print', (err, conn) => { | ||
if (err) { throw err } | ||
|
||
pull(pull.values(['node 1 dialed to node 2 successfully']), conn) | ||
}) | ||
|
||
node2.dial(node3.peerInfo, '/print', (err, conn) => { | ||
if (err) { throw err } | ||
|
||
pull(pull.values(['node 2 dialed to node 3 successfully']), conn) | ||
}) | ||
|
||
node3.dial(node1.peerInfo, '/print', (err, conn) => { | ||
if (err) { | ||
console.log('node 3 failed to dial to node 1 with:', err.message) | ||
} | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 🌟