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

Browser Documentation Helia/Libp2p #103

Closed
wants to merge 8 commits into from

Conversation

TheGreatAlgo
Copy link

Following our discussions at IPFS Thing on implementing Helia (which was incredibly helpful and saved so much time), I wanted to write some top level documentation on implementing it and how to get an in browser node working that supports IPNS and IPFS queries as quickly as possible. It is still missing information and could definitely expand more on it, but I figure this is a good starting point? My goal here was to discuss each component, why its needed and quick example on its implementation.

@TheGreatAlgo TheGreatAlgo requested a review from a team as a code owner April 25, 2023 06:22
Copy link
Contributor

@whizzzkid whizzzkid left a comment

Choose a reason for hiding this comment

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

please fix!


## DataStore and BlockStore

The datastore is used to hold important information relating to peers and DHT records. If you do not define how you want to store it, it will use one in memory.
Copy link
Contributor

Choose a reason for hiding this comment

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

optionally, we can link the datastore implementation that it uses in memory? i.e. https://github.com/ipfs/helia/blob/main/packages/helia/src/storage.ts#L28 I don't think there are plans to move that out and make a standalone package, but good to have a reference.

Copy link
Author

Choose a reason for hiding this comment

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

added


Blockstore is used to hold the blocks on ipfs you want to store locally

```
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
```
```js

ditto for syntax highlighting.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

## Transports

Transports define the method in which we connect to different nodes on the network. When implementing Helia its important to know a couple things.
1. Where Helia will be run (Browser (Firefox, Chrome, Safari), Node)
Copy link
Contributor

Choose a reason for hiding this comment

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

the language can be simplified, Browsers (Firefox, Chrome, Safari), JS runtimes (e.g. node, deno, bun)

Copy link
Author

Choose a reason for hiding this comment

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

fixed

1. Where Helia will be run (Browser (Firefox, Chrome, Safari), Node)
2. Which nodes likely have data you need

The first point is important since tcp is not available in browser for example. It can only be used when running in Nodejs. If you are running in browser, WebTransports are not available in Safari but it is coming to Firefox. https://caniuse.com/webtransport
Copy link
Contributor

Choose a reason for hiding this comment

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

so we need to remove from the examples.

Copy link
Author

Choose a reason for hiding this comment

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

not sure what you mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought I saw an example, maybe I was wrong.


This allows our node to relay traffic to other nodes that you may wish not to be connected directly to the public. Not really helpful in the browser but can be implemented via:

**TO BE FILLED IN**
Copy link
Contributor

Choose a reason for hiding this comment

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

missing?

Copy link
Author

Choose a reason for hiding this comment

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

I am not too familiar with this portion and how it works, was hoping someone more knowledgeable could fill in the gap

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just remove it, or add a TODO.

Comment on lines 142 to 144



Copy link
Contributor

Choose a reason for hiding this comment

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

inconsistent spacing, this does not matter in md but looks better.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

const libp2p = await createLibp2p({
... // placeholder for remaining config
peerDiscovery: [
bootstrap({
Copy link
Contributor

Choose a reason for hiding this comment

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

inconsistent indentation

Copy link
Author

Choose a reason for hiding this comment

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

fixed

In the below example, we will want to load IPNS and IPFS since we will be resolving some IPNS names and fetching content as well.

You will also want to wait a couple seconds to generate all the peers and establish connection to network before trying to access content
```
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

sure, do i do a pull request just on this portion?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, please do that, thanks!

@Faolain
Copy link

Faolain commented May 10, 2023

bumping @whizzzkid whenever you get a moment ^

Copy link
Contributor

@whizzzkid whizzzkid left a comment

Choose a reason for hiding this comment

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

Thanks!

1. Where Helia will be run (Browser (Firefox, Chrome, Safari), Node)
2. Which nodes likely have data you need

The first point is important since tcp is not available in browser for example. It can only be used when running in Nodejs. If you are running in browser, WebTransports are not available in Safari but it is coming to Firefox. https://caniuse.com/webtransport
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought I saw an example, maybe I was wrong.


This allows our node to relay traffic to other nodes that you may wish not to be connected directly to the public. Not really helpful in the browser but can be implemented via:

**TO BE FILLED IN**
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just remove it, or add a TODO.

In the below example, we will want to load IPNS and IPFS since we will be resolving some IPNS names and fetching content as well.

You will also want to wait a couple seconds to generate all the peers and establish connection to network before trying to access content
```
Copy link
Contributor

Choose a reason for hiding this comment

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

yes, please do that, thanks!

@TheGreatAlgo
Copy link
Author

made requested changes and added example to helia examples

@SgtPooki SgtPooki self-requested a review May 24, 2023 16:22
Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

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

This is a great writeup. I know @achingbrain had some questions about this and maybe wanted this to be a blog post, or potentially post in libp2p directly and then link to it from helia, but I'll leave that decision to him.


## Circuit Relay

This allows our node to relay traffic to other nodes that you may wish not to be connected directly to the public. Not really helpful in the browser but can be implemented via:
Copy link
Member

Choose a reason for hiding this comment

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

@achingbrain I saw that we have circuit-relay enabled in the new default libp2p config for the browser. Is this statement true?


This allows our node to relay traffic to other nodes that you may wish not to be connected directly to the public. Not really helpful in the browser but can be implemented via:

**TO DO**
Copy link
Member

Choose a reason for hiding this comment

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

The text here, and content below, needs to be updated.

Copy link
Author

Choose a reason for hiding this comment

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

I am not familiar enough to write this portion. I am still learning helia


## Stream Muxing

It allows multiple conversations on the same line by giving it a unique identifier.
Copy link
Member

@SgtPooki SgtPooki May 25, 2023

Choose a reason for hiding this comment

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

Suggested change
It allows multiple conversations on the same line by giving it a unique identifier.
It allows multiple conversations(Streams) on the same line(Connection) by giving it a unique identifier.

It could be useful to use/translate common libp2p terminology with a legend or something.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 115 to 120
validators: {
ipns: ipnsValidator,
},
selectors: {
ipns: ipnsSelector,
},
Copy link
Member

Choose a reason for hiding this comment

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

This isn't necessary for DHT config to work. It could be beneficial to introduce the ipns config as a subitem of ## DHT

Copy link
Author

Choose a reason for hiding this comment

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

also not too familiar with everything. Still learning and documenting places that caused difficulties for me

Comment on lines 150 to 155
list: [
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
Copy link
Member

Choose a reason for hiding this comment

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

this should be updated to use the same as https://github.com/ipfs/helia/blob/main/packages/helia/src/utils/bootstrappers.ts, which is based off of https://github.com/ipfs/kubo/blob/da28fbc65a2e0f1ce59f9923823326ae2bc4f713/config/bootstrap_peers.go#L17

It might be good to mention the place to look(links above) to keep your bootstrap list up to date.

Copy link
Author

Choose a reason for hiding this comment

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

added


In the below example, we will want to load IPNS and IPFS since we will be resolving some IPNS names and fetching content as well.

You will also want to wait a couple seconds to generate all the peers and establish connection to network before trying to access content
Copy link
Member

Choose a reason for hiding this comment

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

it might be useful to show the users code of how they can do this:

const isHeliaReadyToFetch = async (helia) => {
  // return when helia has peers connected. Can simply wait for peer connection event, or handle manually with interval/other.
}

Copy link
Author

Choose a reason for hiding this comment

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

added a quick example

@Faolain
Copy link

Faolain commented Jul 7, 2023

bumping again @whizzzkid , imo some additional documentation is better than none

Screenshot 2023-07-07 at 6 08 21 PM

@achingbrain
Copy link
Member

achingbrain commented Sep 14, 2023

Thank you so much for opening this PR and for taking the time to respond to feedback.

Everything suggested in it is very sensible, so much so that it's basically become the default browser config you get when you invoke createHelia in a browser.

This is different to early Helia releases which were a lot more bare bones, which felt right at the time but lead to a lot of duplicated configuration for users, some of it incorrect. Instead now default config makes it a lot easier to get started.

This leads to the odd situation where although we're super grateful this PR was opened, the recommendations in the added markdown file are somewhat redundant so I'm going to close this, but thanks again for taking the time to document how to set things up so diligently.

For future documentation efforts we have enabled the wiki on this repo so we can iterate on the docs a bit faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants