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

Preferred way to instantiate Libp2p bundle? Inherit or static create? #811

Closed
a1300 opened this issue Nov 26, 2020 · 4 comments
Closed

Preferred way to instantiate Libp2p bundle? Inherit or static create? #811

a1300 opened this issue Nov 26, 2020 · 4 comments
Labels
kind/support A question or request for support

Comments

@a1300
Copy link
Contributor

a1300 commented Nov 26, 2020

  • Version: 0.29.3
  • Platform: Linux
  • Subsystem:

Type: Question

I found in the examples both the new Libp2p() and Libp2p.create() methods. In the API documentation only the new Libp2p() way is shown.

Are they the same? Is it safe to inherit from Libp2p and add extra methods?

Example with Libp2p.create()

const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},

Example with new Libp2p()

const nodeDialer = new Node({
peerId: idDialer,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
}
})

@vasco-santos
Copy link
Member

Hello @a1300

The preferred way to instantiate Libp2p is using Libp2p.create. When you create Libp2p via its constructor a PeerId must be provided, while create is a more out of the box solution that can receive the peerId or not. If you would like to PR the example dialer to use Libp2p.create, it would be welcome.

Regarding the docs, we actually have both documented in https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#create . The new Libp2p is documented in a toggle that says alternative, as we really don't want people to use that. However, the class being exported enables some use cases where we want to create a Libp2p node but extend it with some behaviour.

@vasco-santos vasco-santos added the kind/support A question or request for support label Nov 26, 2020
@a1300
Copy link
Contributor Author

a1300 commented Nov 26, 2020

Hi @vasco-santos
thanks for the fast answer! Didn't saw the alternative toggle. I think the alternative example (new Libp2p()) is missing a comment that specifies that a peerId is required.

Will send a PR for the dialer example

@vasco-santos
Copy link
Member

I think the alternative example (new Libp2p)) is missing a comment that specifies that a peerId is required.

If you would like to also PR this change, feel free to do it :)

@a1300
Copy link
Contributor Author

a1300 commented Dec 7, 2020

Update docs examples in PR: #814

@a1300 a1300 closed this as completed Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support A question or request for support
Projects
None yet
Development

No branches or pull requests

2 participants