In modern software development, real-time updates are vital, especially for applications that rely on up-to-date information for users. In such scenarios, changes made to the app's database are immediately reflected in the user interface, eliminating the need for manual refreshing or waiting for server responses.
Appwrite is a popular backend-as-a-service platform that simplifies database management, user authentication, account management, and storage. Its real-time functionality is especially vital for chat apps, analytics, live scoreboards, or more complex features like live streaming and collaborative editing.
This blog post illustrates how to use Appwrite's real-time feature to create a racing leaderboard in Nuxt.js. You will style your leaderboard using Appwrite's Pink Design system, giving it a cohesive and visually appealing look.
The source code for this project is available below:
Real-Time Racing Leaderboard | GitHub
Jump ahead:
- Prerequisites
- Setting Up a Nuxt Project
- Setting Up an Appwrite Database
- Implementing Real Time Updates with Appwrite
- Conclusion
Prerequisites
To create a real-time leaderboard in Nuxt.js, it is essential to meet the following requirements:
- Have a solid understanding of JavaScript, Vue.js, CSS, and styling.
- Verify that Node.js is installed on your personal computer (PC) by running
node -v
in your terminal. If not, download and install it from the official website. - Verify that Yarn is installed on your PC by running
yarn -v
in your terminal. If not, install it by runningnpm install --location=global yarn
in your terminal. - Set up a remote Appwrite instance with Appwrite Cloud.
or
- Verify that Docker Desktop is installed on your PC by running
docker -v
in your terminal, or follow this guide to install it. Then set up a local Appwrite instance on your PC by following this blog post. - You can also set up your remote instance on Digital Ocean or Gitpod.
Setting Up a Nuxt Project
You can create a new Nuxt.js project by navigating to your preferred repo in your terminal and running the command below:
npx nuxi init <project name>
However, to speed up the development process, bootstrap your project with this starter template on GitHub. It comes with Nuxt 3, Appwrite's Pink Design, and Appwrite's Nuxt module. It also contains some helper functions in the utils
folder.
Clone your generated repo to your preferred local directory, and run yarn
to install the dependencies listed in package.json
.
Note: If you encounter any errors while installing dependencies, it is most likely because you need to add your Appwrite details—endpoint, project ID, database ID, and collection ID—to a
.env
file. Don't worry about it for now. Set up your Appwrite Database first, then add the necessary details to your.env
file and rerunyarn
in your terminal.
Setting Up an Appwrite Database
If you have an Appwrite Cloud account, click here to open it in your browser and create a new project.
In your Appwrite project, click on Databases, and then click on Create Database to create a new database.
In your newly created database, create a collection.
In the Settings tab of your new collection, scroll down to Update Permissions and set Any to Read.
This ensures that anybody can view the data on your database. If you want your users to create, update, or delete documents in the database, check the necessary boxes.
In your collection, click on the Attributes tab and create some attributes for each document in your collection.
You will create four attributes:
-
car
- String -
carNumber
- Integer -
driver
- String -
duration
- Integer (in seconds)
Seed the database with some data by clicking on the Documents tab and clicking Create document.
Implementing Real Time Updates with Appwrite
Setting up environment variables
In your code editor, create a .env
in your project's root directory or rename .env.example
to .env
. It should contain the text below:
APPWRITE_ENDPOINT=
APPWRITE_PROJECT_ID=
APPWRITE_DATABASE_ID=
APPWRITE_COLLECTION_ID=
Follow this guide to update your .env
file with the correct data from your Appwrite database:
- If you created your database with Appwrite Cloud, your
APPWRITE_ENDPOINT
ishttps://cloud.appwrite.io/v1
. If you're running a local instance, usehttp://localhost/80
as your endpoint. If you hosted on Digital Ocean or Gitpod, use the provided endpoint for your chosen platform. - Store your project's ID in
APPWRITE_PROJECT_ID
, your database ID inAPPWRITE_DATABASE_ID
, and your collection ID inAPPWRITE_COLLECTION_ID
.
In your project's root directory, update nuxt.config.ts
with the code below:
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
runtimeConfig: {
public: {
database: process.env.APPWRITE_DATABASE_ID,
collection: process.env.APPWRITE_COLLECTION_ID,
},
},
modules: ['nuxt-appwrite'],
appwrite: {
endpoint: process.env.APPWRITE_ENDPOINT,
project: process.env.APPWRITE_PROJECT_ID,
},
});
Here, you set up Nuxt.js' runtime config to read the data from your .env
file and set up nuxt-appwrite
module with the correct endpoint and project ID.
Getting Data From Appwrite's Database
Open app.vue
and update the <script>
tag with the code below:
<script setup>
import '@appwrite.io/pink';
import '@appwrite.io/pink-icons';
const config = useRuntimeConfig();
const { account, client, database } = useAppwrite();
// Create an anonymous session if it doesn't already exist
if (account.getSession === null) {
await account.createAnonymousSession();
}
// Get initial data from the database
const { documents } = await database.listDocuments(
config.database,
config.collection
);
// Add racing data to state
const { value: racingData } = useState('racingData', () => sortData(documents));
</script>
Here you did the following:
- Set up runtime config to access data from your
.env
file - Imported
account
,client
, anddatabase
from Appwrite's Nuxt.js module - Created an anonymous Appwrite session so any user can access your database
- Got the initial data from your leaderboard database and destructured it to get the
documents
key; this contains an array of all the documents in your database - Sorted your array of documents by duration using a helper function (
sortData
) and added the sorted array to Nuxt.js' state withracingData
as its variable name
Still in your app.vue
, update <RacingLeaderboard>
in your <template>
tag:
<template>
<NuxtLayout>
<RacingLeaderboard :data="racingData" />
</NuxtLayout>
</template>
If you run the project with yarn dev
and open localhost:3000
in your terminal, you will see the UI below:
Subscribing to Real Time Updates
In the app.vue
<script>
tag, add the code below:
<script>
// ...previous code, do not delete; just add the one below
// Subscribe to real-time updates from the database
onMounted(() => {
try {
client.subscribe(
`databases.${config.database}.collections.${config.collection}.documents`,
(res) => updateState(racingData, res.payload)
);
} catch (error) {
console.log(error);
}
});
<script/>
Here you did the following:
- Set up
onMounted()
to ensure that theclient.subscribe()
method only works when the project is loaded on the browser; this is because it requires thewindow
method, which is only available on the browser - Your
client.subscribe()
method takes in the exact location of your documents as its channel and updates your state with a helper function when there are any changes to the documents in your database
Here's a preview of how it will work.
Notice that you don't need to refresh the browser to see updates to your database.
Conclusion
This blog post demonstrated Appwrite's real-time functionality in a racing leaderboard. The post covered how to set up a Nuxt.js project with Appwrite's Pink Design, set up an Appwrite database, implement real-time updates with Appwrite, and subscribe to real-time updates. Following these steps, developers can add real-time functionality to their applications and create dynamic and engaging user experiences.
Top comments (0)