---
title: "Tutorial: DiceRoller application"
sidebar_position: 3
---
import { MockDiceRollerSample } from "@site/src/components/mockDiceRoller";
In this walkthrough, you'll learn about using the Fluid Framework by examining the DiceRoller application at https://github.com/microsoft/FluidHelloWorld.
To get started, go through the [Quick Start](./quick-start.mdx) guide.
In the DiceRoller app, users are shown a die with a button to roll it.
When the die is rolled, the Fluid Framework syncs the data across clients so everyone sees the same result.
To do this, complete the following steps:
1. [Set up the application](#set-up-the-application).
2. [Create a Fluid container](#create-a-fluid-container).
3. [Write the dice view](#write-the-dice-view).
4. [Connect the view to Fluid data](#connect-the-view-to-fluid-data).
All of the work in this demo will be done in the [app.js](https://github.com/microsoft/FluidHelloWorld/blob/main/src/app.js) file.
## Set up the application
Start by creating a new instance of the Tinylicious client.
Tinylicious is the Fluid Framework's local testing server, and a client is responsible for creating and loading containers.
The app creates Fluid containers using a schema that defines a set of _initial objects_ that will be available in the container.
Learn more about initial objects in [Data modeling](../build/data-modeling.mdx).
Lastly, `root` defines the HTML element that the Dice will render on.
```js
import { SharedTree, TreeViewConfiguration, SchemaFactory, Tree } from "fluid-framework";
import { TinyliciousClient } from "@fluidframework/tinylicious-client";
const client = new TinyliciousClient();
const containerSchema = {
initialObjects: { diceTree: SharedTree },
};
const root = document.getElementById("content");
```
:::note
To create a Fluid application that can be deployed to Azure, check out the [Azure Fluid Relay](../deployment/azure-fluid-relay.mdx).
:::
## Create a Fluid container
Fluid data is stored within containers, and these containers need to be created before other users can load them.
Since creation and loading of containers both happen in the browser, a Fluid application needs to be capable of handling both paths.
### Create a new container
The creation section of the application starts with calling `createContainer` and passing in a schema defining which shared objects will be available on the new `container`.
After a new container is created, default data can be set on the shared objects before the container is attached to the Tinylicious service.
The `"2.0.0"` argument specifies the oldest Fluid Framework client version that the application supports collaborating with.
New applications should specify the highest version supported by every collaborating client.
The `attach` call publishes the container to the Tinylicious service and returns the `id` of the container, which the app can use to load this container on other clients (or this client in a future session).
Once attached, any further changes to the shared objects, made by the rendered app, will be communicated to all collaborators.
The `renderDiceRoller` function is created in a later step.
It renders the UI of the app on the local client.
```js
const createNewDice = async () => {
const { container } = await client.createContainer(containerSchema, "2.0.0");
const dice = container.initialObjects.diceTree.viewWith(treeViewConfiguration);
dice.initialize(new Dice({ value: 1 }));
const id = await container.attach();
renderDiceRoller(dice.root, root);
};
```
### Loading an existing container
Loading a container is more straightforward than creating a new one.
When loading, the container already contains data, and is already attached, so those steps are irrelevant.
You need only to pass the `id` of the container you wish to load in the `getContainer()` function along with the same schema used when creating the container.
```js
const loadExistingDice = async (id) => {
const { container } = await client.getContainer(id, containerSchema, "2.0.0");
const dice = container.initialObjects.diceTree.viewWith(treeViewConfiguration);
renderDiceRoller(dice.root, root);
};
```
### Switching between loading and creating
The app supports both creating a new container and loading an existing container using its `id`.
But, the app needs to know whether the container already exists.
There are many ways of determining this.
This sample app stores the container ID in the URL hash.
If the URL has a hash, the app will load that existing container.
Otherwise, the app creates a new container, attaches it, and sets the returned `id` as the hash.
The decision logic is implemented in a `start` function which is immediately called, catching any errors that are returned.
```js
async function start() {
if (location.hash) {
await loadExistingDice(location.hash.substring(1));
} else {
const id = await createNewDice();
location.hash = id;
}
}
start().catch((error) => console.error(error));
```
## Write the dice view
The Fluid Framework is agnostic about view frameworks and works well with React, Vue, Angular and web components.
This example uses standard HTML/DOM methods to render a view.
The `renderDiceRoller` function runs only when the container is created or loaded.
It appends the `template` to the passed in HTML element, and creates a working dice roller with a random dice value each time the "Roll" button is clicked on a client.
```js
const template = document.createElement("template");
template.innerHTML = `