You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/how-to/ipfs-in-web-apps.md
+18-25Lines changed: 18 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,17 +5,19 @@ description: How to develop applications that use IPFS in web browsers, includin
5
5
6
6
# IPFS in web-applications and resource-constrained environments
7
7
8
-
This guide covers the key operations of IPFS in the context of web applications, including addressing, retrieving, and providing.
8
+
In this guide you will learn how to use IPFS in web applications, including addressing, retrieving, and providing.
9
9
10
-
For demonstration purposes, this guide uses[Helia](https://github.com/ipfs/helia), the most actively maintained library for using IPFS on the web and is the recommended library for most use cases.
10
+
In this guide, you will use[Helia](https://github.com/ipfs/helia), the most actively maintained TypeScript IPFS library for use on the web and the recommended library for most use cases.
11
11
12
-
## Challenges with IPFS on the Web
12
+
> **Note:** this guide is focused solely on using IPFS for data within a web application. It does _not_ cover using IPFS for static website distribution with IPFS Gateways.
13
+
14
+
## Challenges with IPFS on the web
13
15
14
16
IPFS allows you to fetch data by CID from multiple providers without being reliant on a single authoritative server.
15
17
16
-
However, making all of this work on the Web is tricky due to networking constraints. Browsers impose many restrictions on Web apps, for example, opening TCP/UDP connections is not possible. Instead, Web apps are constrained to HTTP, WebSockets, WebRTC, and most recently WebTransport.
18
+
However, making all of this work on the web is tricky due to networking constraints. Browsers impose many restrictions on web apps, for example, opening TCP/UDP connections is not possible. Instead, web apps are constrained to HTTP, WebSockets, WebRTC, and most recently WebTransport.
17
19
18
-
There are good reasons for this like security and resource management, but ultimately, it means that using IPFS on the Web is different to native binaries.
20
+
There are good reasons for this like security and resource management, but ultimately, it means that using IPFS on the web is different to native binaries.
19
21
20
22
## Key IPFS operations: Addressing, Retrieving, and Providing
21
23
@@ -25,8 +27,7 @@ As a developer, IPFS exposes three main operations for interacting with the netw
25
27
-**Providing data by CID**: making data addressed by a CID retrievable by other peers, either by running a node or with a pinning service.
26
28
-**Retrieving data by CID**: given a CID, IPFS finds providers (peers who share the block), connects to them, fetches the blocks, and verifies.
27
29
28
-
29
-
## Addressing by CID
30
+
## Addressing data by CID
30
31
31
32
As mentioned above, the first step in the [lifecycle of data in IPFS](../concepts/lifecycle.md) is to address it by CID.
32
33
@@ -37,29 +38,22 @@ When addressing data by [CIDs](https://proto.school/anatomy-of-a-cid/03) you wil
37
38
-[unixfs](../concepts/file-systems.md#unix-file-system-unixfs) for files and directories.
38
39
-[dag-cbor](../concepts/glossary.md#dag-cbor) for json-like structured data with binary encoding. DAG-CBOR is an extension of CBOR that adds a "link" type for CIDs, allowing for the creation of interlinked CBOR objects (which can be used to form larger linked data structures).
39
40
40
-
As you can see, there are multiple ways to address data by CID.
41
+
### CID Determinism
41
42
42
-
One important thing to note is that **the same data can result in different CIDs** depending on a number of factors, including the hash function, the multicodec you use, and the way you encode the data. **This is especially true for files**, where the same file, hash function and multicodec can still result in different CIDs depending on the different options that UnixFS supports. See the [forum discussion](https://discuss.ipfs.tech/t/should-we-profile-cids/18507) for more details and a possible solution.
43
+
One important thing to note is that **the same data can result in different CIDs** depending on a number of factors, including the hash function, the multicodec you use, and the multicodec. **This is especially true for files**, where the same file, hash function and multicodec can still result in different CIDs depending on the different options that UnixFS supports. See the [forum discussion](https://discuss.ipfs.tech/t/should-we-profile-cids/18507) for more details.
43
44
44
45
### Example: Addressing an object by CID with dag-cbor
45
46
46
47
For example, to address an object by CID with the `dag-cbor` multicodec and `sha2-256` hash function, you can use the following code using [Helia](https://github.com/ipfs/helia):
<iframeheight="300"style="width: 100%;"scrolling="no"title="Addressing an object by CID with Helia and dag-cbor"src="https://codepen.io/2color/embed/xbKpJKx?default-tab=js%2Cresult"frameborder="no"loading="lazy"allowtransparency="true"allowfullscreen="true">
50
+
See the Pen <a href="https://codepen.io/2color/pen/xbKpJKx">
51
+
Addressing an object by CID with Helia and dag-cbor</a> by Daniel Norman (<a href="https://codepen.io/2color">@2color</a>)
52
+
</iframe>
59
53
60
54
### Example: Addressing a file by CID with unixfs
61
55
62
-
<iframeheight="500"style="width: 100%;"scrolling="no"title="Addressing an image by CID with Helia and UnixFS"src="https://codepen.io/2color/embed/zxONqPj?default-tab=html%2Cresult"frameborder="no"loading="lazy"allowtransparency="true"allowfullscreen="true">
56
+
<iframeheight="500"style="width: 100%;"scrolling="no"title="Addressing an image by CID with Helia and UnixFS"src="https://codepen.io/2color/embed/zxONqPj?default-tab=js%2Cresult"frameborder="no"loading="lazy"allowtransparency="true"allowfullscreen="true">
63
57
See the Pen <a href="https://codepen.io/2color/pen/zxONqPj">
64
58
Addressing an image by CID with Helia and UnixFS</a> by Daniel Norman (<a href="https://codepen.io/2color">@2color</a>)
65
59
</iframe>
@@ -72,7 +66,7 @@ If you want to data to be retrievable by other peers on [Mainnet](../concepts/gl
72
66
73
67
### You probably don't want to provide data from a browser
74
68
75
-
Browsers make for lousy servers. It's difficult to make a Web page "dialable", i.e. allow network incoming connections from other computers. There's one exception, namely WebRTC, however, it has many caveats.
69
+
Browsers make for lousy servers. It's difficult to make a Web page a server, i.e. allow network incoming connections from other computers. There's one exception, namely WebRTC, however, it has many caveats.
76
70
77
71
For this reason, you should almost never count on providing data from a browser to work.
78
72
@@ -82,12 +76,11 @@ Instead, you should provide data from a long-running server that runs reliably a
82
76
83
77
CAR files offer a serialized representation of content-addressed resources in one single concatenated stream, alongside a header that describes that content. This makes them a great way to store content-addressed data in a way that is easy to transport and store.
84
78
85
-
86
79
## Retrieval
87
80
88
81
### With Verified-fetch
89
82
90
-
<iframeheight="500"style="width: 100%;"scrolling="no"title="Fetch an image on IPFS Mainnet @helia/verified-fetch"src="https://codepen.io/2color/embed/QWXKZGx?default-tab=html%2Cresult"frameborder="no"loading="lazy"allowtransparency="true"allowfullscreen="true">
83
+
<iframeheight="500"style="width: 100%;"scrolling="no"title="Fetch an image on IPFS Mainnet @helia/verified-fetch"src="https://codepen.io/2color/embed/QWXKZGx?default-tab=js%2Cresult"frameborder="no"loading="lazy"allowtransparency="true"allowfullscreen="true">
91
84
See the Pen <a href="https://codepen.io/2color/pen/QWXKZGx">
92
-
Fetch an image on IPFS Mainnet @helia/verified-fetch</a> by Daniel Norman (<a href="https://codepen.io/2color">@2color</a>)
85
+
Fetch an image on IPFS Mainnet @helia/verified-fetch</a> by Daniel Norman
0 commit comments