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

Updating instructions in examples/transports tut #1200

Closed
wants to merge 4 commits into from
Closed
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
40 changes: 19 additions & 21 deletions examples/transports/1.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
/* eslint-disable no-console */
'use strict'

import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { Noise } from '@chainsafe/libp2p-noise'
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const { NOISE } = require('@chainsafe/libp2p-noise')

const createNode = async () => {
const node = await createLibp2p({
const node = await Libp2p.create({
addresses: {
// To signal the addresses we want to be available, we use
// the multiaddr format, a self describable address
listen: [
'/ip4/0.0.0.0/tcp/0'
]
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [
new TCP()
],
connectionEncryption: [
new Noise()
]
modules: {
transport: [TCP],
connEncryption: [NOISE]
}
})

await node.start()
return node
}

;(async () => {
const node = await createNode()

console.log('node has started (true/false):', node.isStarted())
console.log('listening on:')
node.getMultiaddrs().forEach((ma) => console.log(ma.toString()))
})();
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))

return node
}

createNode().catch(err => {
console.error(err)
process.exit(1)
})
52 changes: 30 additions & 22 deletions examples/transports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,52 @@ Then, in your favorite text editor create a file with the `.js` extension. I've
First thing is to create our own libp2p node! Insert:

```JavaScript
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { Noise } from '@chainsafe/libp2p-noise'
'use strict'

const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const { NOISE } = require('@chainsafe/libp2p-noise')

const createNode = async () => {
const node = await createLibp2p({
const node = await Libp2p.create({
addresses: {
// To signal the addresses we want to be available, we use
// the multiaddr format, a self describable address
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [
new TCP()
],
connectionEncryption: [
new Noise()
]
modules: {
transport: [ TCP ],
connEncryption: [ NOISE ]
}
})

await node.start()
return node
}
```

Now that we have a function to create our own libp2p node, let's create a node with it.
Now that we have a function to create our own libp2p node, let's create a node with it. Inside of the createNode() async function, before the `return node` and after the `await node.start() ` statement, add in some logging:

```JavaScript
const createNode = async () => {
...
await node.start()

console.log('node has started (true/false):', node.isStarted())
console.log('listening on:')
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))

return node
}
```

Now, you want to call the `createNode()` function, and you can also add in error handling:

```JavaScript
const node = await createNode()

// At this point the node has started
console.log('node has started (true/false):', node.isStarted())
// And we can print the now listening addresses.
// If you are familiar with TCP, you might have noticed
// that we specified the node to listen in 0.0.0.0 and port
// 0, which means "listen in any network interface and pick
// a port for me
console.log('listening on:')
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
createNode().catch(err => {
console.error(err)
process.exit(1)
})
Comment on lines +48 to +69
Copy link
Member

Choose a reason for hiding this comment

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

Can you revert the changes other than this? We want to keep examples using the latest versions of libp2p dependencies, using ESM imports, etc.

```

Running this should result in something like:
Expand Down