--- layout: learn description: Learn how to implement Gaia in a DApp permalink: /:collection/:path.html --- # Blockstack Storage Tutorial {:.no_toc} In this tutorial, you build a micro-blogging application using multi-player Gaia storage. Gaia is Blockstack's [decentralized high-performance storage system](https://github.com/blockstack/gaia). The tutorial contains the following topics: * TOC {:toc} This tutorial does not teach you about authentication. That is covered in depth [in the hello-blockstack tutorial](hello-blockstack). {% include note.html content="This tutorial was written on macOS High Sierra 10.13.4. If you use a Windows or Linux system, you can still follow along. However, you will need to \"translate\" appropriately for your operating system. Additionally, this tutorial assumes you are accessing the Blockstack Browser web application via Chrome. The application you build will also work with a local installation and/or with browsers other than Chrome. " %} ## About this tutorial and the prerequisites you need The storage application you build with this tutorial is a React.js application that is completely decentralized and server-less. While not strictly required to follow along, basic familiarity with React.js is helpful. When complete, the app is capable of the following: - authenticating users using Blockstack - posting new statuses - displaying statuses in the user profile - looking up the profiles and statuses of other users The basic identity and storage services are provided by `blockstack.js`. To test the application, you need to have already [registered a Blockstack ID](ids-introduction). For this tutorial, you will use the following tools: - Node.js v10 or higher is recommended the minimum supported version is Node.js v8. - `blockstack.js` to authenticate the user and work with the user's identity/profile information Before you begin, verify you have the correct version of Node.js and its tools installed. ```bash $ node -v v12.10.0 $ which npm npx /usr/local/bin/npm /usr/local/bin/npx ``` If you don't have these installed, take a moment to install or upgrade as needed. Also, make sure you have [created at least one Blockstack ID]({{ site.baseurl }}/browser/ids-introduction.html#create-an-initial-blockstack-id). You'll use this ID to interact with the application. Finally, if you get stuck at any point while working on the tutorial, the completed [source code is available for you](https://github.com/yknl/publik) to check your work against. You can also try out the [live build](https://publik.test-blockstack.com) of the app. ## Generate and launch the public application {% include scaffolding.md %} In this section, you build an initial React application called Publik. 1. Create a the `publik` directory. ```bash mkdir publik ``` 2. Change into your new directory. ```bash cd publik ``` 3. Use the Blockstack application generator to create your initial `publik` application. ```bash $ npx generator-blockstack --react npx: installed 338 in 13.792s create package.json create .gitignore create webpack.config.js create netlify.toml create firebase.json ... I'm all done. Running npm install for you to install the required dependencies. If this fails, try running the command yourself. > fsevents@1.2.9 install /private/tmp/testymc/node_modules/fsevents > node install added 775 packages from 455 contributors and audited 9435 packages in 20.934s found 0 vulnerabilities ``` Depending on your environment you may have some warnings with the installation. Optionally, you can fix these before continuing to the next section. 4. Run the initial application. ```bash npm run start ``` The system may prompt you to accept incoming connections. ![Network Connection](./images/network-connections.gif) 5. If it does, choose **Allow**. 6. Your browswer –– Chrome by default –– will open to `http://127.0.0.1:3000/`. You should see a simple React app. ![](images/initial-app.png) 8. Choose **Sign In with Blockstack**. The application tells you it will **Read your basic info**. ![](images/login.png) Leave your new application running and move onto the next section. ## Add the `publish_data` scope to sign in requests Any Blockstack app can use Gaia storage, but those apps that need to share data publicly must add themselves to the user's `profile.json` file. The Blockstack Browser does this automatically when the `publish_data` scope is requested during authentication. For this application, the user files stored on Gaia are made visible to others via the `apps` property in the user's `profile.json` file. Modify your authentication request to include the `publish_data` scope. 1. Open `src/components/App.js` file. 2. Locate the `AppConfig` declaration near the beginning of the file. ```javascript const appConfig = new AppConfig() ``` 3. Change it to this: ```javascript const appConfig = new AppConfig(['store_write', 'publish_data']) ``` By default, authentication requests include the `store_write` scope which enables storage. This is what allows you to store information to Gaia. Adding the `publish_data` scope allows your app to share data between users. 4. Save your changes. 5. Go back to your app at `http://127.0.0.1:3000/`. 6. Log out and sign in again. The authentication request now prompts the user for permission to **Publish data stored for the app**. ![](images/publish-data-perm.png) ## Understand Gaia storage methods Once you authenticate a user with `store_write` and `publish_data`, you can begin to manage data for your users. Blockstack JS provides two methods within the `UserSession` class, `UserSession.getFile` and `UserSession.putFile` for interacting with Gaia storage. The storage methods support all file types. This means you can store markdown, JSON, or even a custom format. You can create a meaningful and complex data layer using these two methods. Before creating an application, consider fundamental data architecture and make some decisions about how you’re modeling data. For example, consider building a simple grocery list app. A user should be able to create, read, update, and delete grocery lists. A single file collection stores items as an array nested inside each grocery list: ```js // grocerylists.json { "3255": { "items": [ "1 Head of Lettuce", "Haralson apples" ] }, // ...more lists with items } ``` This is conceptually the simplest way to manage grocery lists. When you read a `/grocerylists.json` file with `getFile()`, you get back one or more grocery lists and their items. When you write a single list, the `putFile()` method overwrites the entire list. So, a write operation for a new or updated grocery list must submit all existings lists as well. Further, because this runs on the client where anything can go wrong. If the client-side code encounters a parsing error with a user-input value and you could overwrite the entire file with: `line 6: Parsing Error: Unexpected token.` Further, a single file makes pagination impossible and if your app stores a single file for all list you have less control over file permissions. To avoid these issues, you can create an index file that stores an array of IDs. These IDs point to a name of another file in a `grocerylists` folder. ![](images/multiple-lists.png) This design allows you to get only the files you need and avoid accidentally overwriting all lists. Further, you’re only updating the index file when you add or remove a grocery list; updating a list has no impact. ## Add support for user status submission and lookup In this step, you add three `blockstack.js` methods that support posting of "statuses". These are the `UserSession.putFile`, `UserSession.getFile`, and `lookupProfile` methods. 1. Open the `src/components/Profile.js` file. 2. Replace the initial state in the `constructor()` method so that it holds the key properties required by the app. This code constructs a Blockstack `Person` object to hold the profile. Your constructor should look like this: ```javascript constructor(props) { super(props); this.state = { person: { name() { return 'Anonymous'; }, avatarUrl() { return avatarFallbackImage; }, }, username: "", newStatus: "", statuses: [], statusIndex: 0, isLoading: false }; } ``` 3. Locate the `render()` method. 4. Modify the `render()` method to add a text input and submit button to the by replacing it with the code below: The following code renders the `person.name` and `person.avatarURL` properties from the profile on the display: ```javascript render() { const { handleSignOut, userSession } = this.props; const { person } = this.state; const { username } = this.state; return ( !userSession.isSignInPending() && person ?

{ person.name() ? person.name() : 'Nameless Person' }

{username}  |  (Logout)