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: use Libp2p.create() in examples (#811) #814

Merged
merged 2 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 39 additions & 8 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,25 @@ For Libp2p configurations and modules details read the [Configuration Document](

```js
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')

async function main () {
// specify options
const options = {
modules: {
transport: [TCP],
streamMuxer: [MPLEX],
connEncryption: [NOISE]
}
}

// specify options
const options = {}
// create libp2p
const libp2p = await Libp2p.create(options)
}

// create libp2p
const libp2p = await Libp2p.create(options)
main()
```

Note: The [`PeerId`][peer-id] option is not required and will be generated if it is not provided.
Expand All @@ -131,12 +144,30 @@ As an alternative, it is possible to create a Libp2p instance with the construct

```js
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const MPLEX = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const PeerId = require('peer-id')

async function main () {
const peerId = await PeerId.create();

// specify options
// peerId is required when Libp2p is instantiated via the constructor
const options = {
peerId,
modules: {
transport: [TCP],
streamMuxer: [MPLEX],
connEncryption: [NOISE]
}
}

// specify options
const options = {}
// create libp2p
const libp2p = new Libp2p(options)
}

// create libp2p
const libp2p = new Libp2p(options)
main()
```

Required keys in the `options` object:
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const PeerId = require('peer-id')
const multiaddr = require('multiaddr')
const Node = require('./libp2p-bundle')
const createLibp2p = require('./libp2p-bundle')
const { stdinToStream, streamToConsole } = require('./stream')

async function run() {
Expand All @@ -13,7 +13,7 @@ async function run() {
])

// Create a new libp2p node on localhost with a randomly chosen port
const nodeDialer = new Node({
const nodeDialer = await createLibp2p({
peerId: idDialer,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
Expand Down
25 changes: 10 additions & 15 deletions examples/chat/src/libp2p-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ const { NOISE } = require('libp2p-noise')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../..')

class Node extends libp2p {
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP,
WS
],
streamMuxer: [ mplex ],
connEncryption: [ NOISE ]
}
}

super(defaultsDeep(_options, defaults))
async function createLibp2p(_options) {
const defaults = {
modules: {
transport: [TCP, WS],
streamMuxer: [mplex],
connEncryption: [NOISE],
},
}

return libp2p.create(defaultsDeep(_options, defaults))
}

module.exports = Node
module.exports = createLibp2p
4 changes: 2 additions & 2 deletions examples/chat/src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
/* eslint-disable no-console */

const PeerId = require('peer-id')
const Node = require('./libp2p-bundle.js')
const createLibp2p = require('./libp2p-bundle.js')
const { stdinToStream, streamToConsole } = require('./stream')

async function run() {
// Create a new libp2p node with the given multi-address
const idListener = await PeerId.createFromJSON(require('./peer-id-listener'))
const nodeListener = new Node({
const nodeListener = await createLibp2p({
peerId: idListener,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/10333']
Expand Down
5 changes: 2 additions & 3 deletions examples/echo/src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* Dialer Node
*/

const multiaddr = require('multiaddr')
const PeerId = require('peer-id')
const Node = require('./libp2p-bundle')
const createLibp2p = require('./libp2p-bundle')
const pipe = require('it-pipe')

async function run() {
Expand All @@ -17,7 +16,7 @@ async function run() {
])

// Dialer
const dialerNode = new Node({
const dialerNode = await createLibp2p({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
Expand Down
25 changes: 10 additions & 15 deletions examples/echo/src/libp2p-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ const { NOISE } = require('libp2p-noise')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../..')

class Node extends libp2p {
constructor (_options) {
const defaults = {
modules: {
transport: [
TCP,
WS
],
streamMuxer: [ mplex ],
connEncryption: [ NOISE ]
}
}

super(defaultsDeep(_options, defaults))
async function createLibp2p(_options) {
const defaults = {
modules: {
transport: [TCP, WS],
streamMuxer: [mplex],
connEncryption: [NOISE],
},
}

return libp2p.create(defaultsDeep(_options, defaults))
}

module.exports = Node
module.exports = createLibp2p
4 changes: 2 additions & 2 deletions examples/echo/src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
*/

const PeerId = require('peer-id')
const Node = require('./libp2p-bundle')
const createLibp2p = require('./libp2p-bundle')
const pipe = require('it-pipe')

async function run() {
const listenerId = await PeerId.createFromJSON(require('./id-l'))

// Listener libp2p node
const listenerNode = new Node({
const listenerNode = await createLibp2p({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/10333']
},
Expand Down