# IPFS in web applications and resource-constrained environments
In this guide you will learn how to use IPFS using JavaScript/TypeScript in web applications, including addressing data with CIDs, retrieval by CID, working with CAR files, and the nuances of providing.
For this, you will use Helia (opens new window), the most actively maintained implementation of IPFS in TypeScript for use on the web.
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.
# Challenges with IPFS on the web
IPFS allows you to fetch data by CID from multiple providers without being reliant on a single authoritative server.
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.
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.
# Key IPFS operations: Addressing, Retrieving, and Providing
As a developer, IPFS exposes three main operations for interacting with the network:
- Addressing data with CIDs (also known as merkleizing): taking arbitrary data and encoding so its addressable by CID. For example, given a directory of files, merkleizing it so it can be addressed and retrieved by CID.
- Retrieving data by CID: given a CID, IPFS finds providers (peers who share the block), connects to them, fetches the blocks, and verifies that the retrieved data is what the CID represents.
- Providing data by CID: making data addressed by a CID retrievable by other peers, either by running a node or with a pinning service.
# Addressing data by CID
As mentioned above, the first step in the lifecycle of data in IPFS is to address it by CID.
When addressing data by CIDs you will need to choose:
- hash function. For use in browsers, the default and recommended hash function is
sha2-256
which is also the default for Helia (opens new window). - multicodec, which is the format of the data you are addressing and is used to help decode data. CIDs support a wide range of multicodecs, but for most intents and purposes, you will likely either want use:
# CID Determinism
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, and the multicodec you use. 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 on CID profiles (opens new window) and the DASL (opens new window) initiative for more for more information on the nature of this problem and how the community is addressing it.
For a visual demonstration of this, try the DAG Builder (opens new window), which visualises how files are addressed by CID with UnixFS and demonstrates how the same file can result in different CIDs, depending on the different options that UnixFS supports.
# Example: Addressing an object by CID with dag-cbor
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 (opens new window):
# Example: Addressing a file by CID with UnixFS
# Retrieval
From a high level, there are several ways to retrieve data with IPFS in web applications:
- Using the Verified Fetch (opens new window) library, which was modelled after the Fetch API (opens new window) and returns Response (opens new window) objects, with the main difference being that it allows you to fetch data by CID, abstracting away the details of content routing, transports and retrieval. For more examples and background see the release blog post (opens new window).
- Using the Helia (opens new window) library, which is the foundation for the
@helia/verified-fetch
library, and provides a more comprehensive and modular API for interacting with the IPFS network, beyond just retrieval. - Using public recursive gateways, e.g.
ipfs.io
with HTTP. This is not recommended for most use cases, because it forgoes the verifiability and trustlessness enabled by content addressing. Granted, it might be the easiest way to retrieve data in a web application, but is also the most fraught with security and centralization concerns.
# Example: Image retrieval with Verified Fetch
# Providing data
For data to be retrievable by other peers on IPFS Mainnet it will need to be uploaded to a pinning service or an IPFS node.
When possible, it's best to rely on client-side merkleization to address data by CID and then upload it to a pinning service or a node. CAR files are a great way to do this, though they are not supported by all pinning services.
# You probably don't want to provide data from a browser
Browsers make for lousy servers. It's difficult to make a Web page a server, i.e. allow network incoming connections from other computers. WebRTC is the only exception, however, it has many caveats, and doesn't work in all networks.
For this reason, you should never count on providing data from a browser to work.
Instead, you should provide data from a long-running server that runs reliably and has a public IP. That can be a Kubo node that you run, or a pinning service.
# CAR files
The Content Archive format is a way of packaging up content addressed data into archive files that can be easily stored and transferred over the network. You can think of them like TAR files that are designed for storing collections of content addressed data.
So why would you want to use CAR files?
One of the main reasons is related to CID determinism. As mentioned above, the same data can result in different CIDs, which can make it difficult to verify data without its content addressed representation. By packaging up the data into a CAR file, you can upload the CAR to multiple pinning services and nodes knowing they are providing the same CIDs
CAR files are a great way to store content-addressed data in a way that is easy to transport and store, and Helia (and other implementations) allow you to both export and import any data you've addressed by CID into a CAR file.
At the time of writing, not all pinning services support CAR files, but it is a feature that is being added to more and more services. Therefore, it is a good idea to check the documentation for the pinning service you are using to see if it supports CAR files.
# Conclusion
This guide has covered the essential aspects of using IPFS in web applications:
- The main operations: addressing/merkleizing data with CIDs, retrieving data, and providing data.
- The challenges and limitations of using IPFS in browser environments.
- Practical examples using modern tools like Helia (opens new window) and Verified Fetch (opens new window).
- Best practices for handling data persistence through pinning services and CAR files.
When building web applications with IPFS, remember these key takeaways:
- Use client-side merkleization (addressing) when adding new data to IPFS, but rely on pinning services or IPFS nodes for providing data.
- Be mindful of CID determinism when working with files and structured data.
- Consider using CAR files where possible for storage and transport of content-addressed data.
- Use Verified Fetch for simple retrieval or Helia for more complex IPFS interactions.
By following these guidelines, you can reap the benefits of IPFS while working within the constraints of the web.