Skip to content

Commit 19d67b9

Browse files
committed
docs(examples): transports 1
1 parent a1e5448 commit 19d67b9

File tree

6 files changed

+147
-4
lines changed

6 files changed

+147
-4
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [Bundles](#bundles)
3131
- [Usage](#usage)
3232
- [Install](#install)
33+
- [Usage](#usage)
3334
- [API](#api)
3435
- [Development](#development)
3536
- [Tests](#tests)
@@ -46,7 +47,7 @@ We are in the process of writting better documentation, blog posts, tutorials an
4647
- [libp2p.io](https://libp2p.io)
4748
- [Specification (WIP)](https://github.com/libp2p/specs)
4849
- Talks
49-
- [`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)
50+
- [`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)
5051
- Articles
5152
- [The overview of libp2p](https://github.com/libp2p/libp2p#description)
5253

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

7273
## Usage
7374

75+
### [Tutorials and Examples](/examples)
76+
77+
You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarions.
78+
7479
### Extending libp2p skeleton
7580

7681
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).

examples/transports/1.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const libp2p = require('libp2p')
4+
const TCP = require('libp2p-tcp')
5+
const PeerInfo = require('peer-info')
6+
const waterfall = require('async/waterfall')
7+
8+
class MyBundle extends libp2p {
9+
constructor (peerInfo) {
10+
const modules = {
11+
transport: [new TCP()]
12+
}
13+
super(modules, peerInfo)
14+
}
15+
}
16+
17+
let node
18+
19+
waterfall([
20+
(cb) => PeerInfo.create(cb),
21+
(peerInfo, cb) => {
22+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
23+
node = new MyBundle(peerInfo)
24+
node.start(cb)
25+
}
26+
], (err) => {
27+
if (err) { throw err }
28+
29+
console.log('node has started (true/false):', node.isOn())
30+
console.log('listening on:')
31+
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
32+
})

examples/transports/2.js

Whitespace-only changes.

examples/transports/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [Transports](http://libp2p.io/implementations/#transports)
2+
3+
libp2p doesn't make assumptions for you, instead, it enables you as the developer of the application to pick the modules you need to run your application, which can vary depending on the runtime you are executing. A libp2p node can use one or more Transports to dial and listen for Connections. These transports are modules that offer a clean interface for dialing and listening, defined by the [interface-transport](https://github.com/libp2p/interface-transport) specification. Some examples of possible transports are: TCP, UTP, WebRTC, QUIC, HTTP, Pigeon and so on.
4+
5+
A more complete definition of what is a transport can be found on the [interface-transport](https://github.com/libp2p/interface-transport) specification. A way to recognize a candidate transport is through the badge:
6+
7+
[![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)
8+
9+
## 1. Creating a libp2p Bundle with TCP
10+
11+
When using libp2p, you always want to create your own libp2p Bundle, that is, pick your set of modules and create your network stack with the properties you need. In this example, we will create a bundle with TCP. You can find the complete solution on the file [1.js](/1.js).
12+
13+
You will need 4 deps total, so go ahead and install all of them with:
14+
15+
```
16+
> npm install libp2p libp2p-tcp peer-info async
17+
```
18+
19+
Then, on your favorite text editor create a file with the `.js` extension. I've called mine `1.js`.
20+
21+
First thing is to create our own bundle! Insert:
22+
23+
```JavaScript
24+
'use strict'
25+
26+
const libp2p = require('libp2p')
27+
const TCP = require('libp2p-tcp')
28+
const PeerInfo = require('peer-info')
29+
const waterfall = require('async/waterfall')
30+
31+
// This MyBundle class is your libp2p bundle packed with TCP
32+
class MyBundle extends libp2p {
33+
constructor (peerInfo) {
34+
// modules is a JS object that will describe the components
35+
// we want for our libp2p bundle
36+
const modules = {
37+
transport: [new TCP()]
38+
}
39+
super(modules, peerInfo)
40+
}
41+
}
42+
```
43+
44+
Now that we have our own MyBundle class that extends libp2p, let's create a node with it. We will use `async/waterfall` just for code structure, but you don't need to. Append to the same file:
45+
46+
```JavaScript
47+
let node
48+
49+
waterfall([
50+
// First we create a PeerInfo object, which will pack the
51+
// info about our peer. Creating a PeerInfo is an async
52+
// operation because we use the WebCrypto API
53+
// (yeei Universal JS)
54+
(cb) => PeerInfo.create(cb),
55+
(peerInfo, cb) => {
56+
// To signall the addresses we want to be available, we use
57+
// the multiaddr format, a self describable address
58+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
59+
// Now we can create a node with that PeerInfo object
60+
node = new MyBundle(peerInfo)
61+
// Last, we start the node!
62+
node.start(cb)
63+
}
64+
], (err) => {
65+
if (err) { throw err }
66+
67+
// At this point the node has started
68+
console.log('node has started (true/false):', node.isOn())
69+
// And we can print the now listening addresses.
70+
// If you are familiar with TCP, you might have noticed
71+
// that we specified the node to listen in 0.0.0.0 and port
72+
// 0, which means "listen in any network interface and pick
73+
// a port for me
74+
console.log('listening on:')
75+
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
76+
})
77+
```
78+
79+
Running this should result in somehting like:
80+
81+
```bash
82+
> node 1.js
83+
node has started (true/false): true
84+
listening on:
85+
/ip4/127.0.0.1/tcp/61329/ipfs/QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ
86+
/ip4/192.168.2.156/tcp/61329/ipfs/QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ
87+
```
88+
89+
That `QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ` is the PeerId that was created during the PeerInfo generation.
90+
91+
## 2. Dialing from one node to another node
92+
93+
Now that we have our bundle, let's create two nodes and make them dial to each other! You can find the complete solution at [2.js](/2.js).
94+
95+
96+
97+
## 3. Using multiple transports
98+
99+
- show TCP + websockets
100+
101+
## 4. How to create a new libp2p transport
102+
103+
- interface-transport
104+
- follow the interface
105+
- show other examples (webrtc, utp, udt (wip), etc)
106+
-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "libp2p",
33
"version": "0.9.1",
4-
"description": "JavaScript Skeleton for libp2p bundles",
4+
"description": "JavaScript baseclass for libp2p bundles",
55
"main": "src/index.js",
66
"scripts": {
77
"test": "gulp test",

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Node extends EventEmitter {
3333
this.swarm = new Swarm(this.peerInfo, this.peerBook)
3434

3535
// Attach stream multiplexers
36-
if (this.modules.connection.muxer) {
36+
if (this.modules.connection && this.modules.connection.muxer) {
3737
let muxers = this.modules.connection.muxer
3838
muxers = Array.isArray(muxers) ? muxers : [muxers]
3939
muxers.forEach((muxer) => this.swarm.connection.addStreamMuxer(muxer))
@@ -54,7 +54,7 @@ class Node extends EventEmitter {
5454
}
5555

5656
// Attach crypto channels
57-
if (this.modules.connection.crypto) {
57+
if (this.modules.connection && this.modules.connection.crypto) {
5858
let cryptos = this.modules.connection.crypto
5959
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
6060
cryptos.forEach((crypto) => {

0 commit comments

Comments
 (0)