|
| 1 | +--- |
| 2 | +title: Nuxt Quickstart |
| 3 | +sidebar_label: Nuxt |
| 4 | +slug: /quickstarts/nuxt |
| 5 | +hide_table_of_contents: true |
| 6 | +--- |
| 7 | + |
| 8 | +import { InstallCardLink } from "@site/src/components/InstallCardLink"; |
| 9 | +import { StepByStep, Step, StepText, StepCode } from "@site/src/components/Steps"; |
| 10 | + |
| 11 | + |
| 12 | +Get a SpacetimeDB Nuxt app running in under 5 minutes. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- [Node.js](https://nodejs.org/) 18+ installed |
| 17 | +- [SpacetimeDB CLI](https://spacetimedb.com/install) installed |
| 18 | + |
| 19 | +<InstallCardLink /> |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +<StepByStep> |
| 24 | + <Step title="Create your project"> |
| 25 | + <StepText> |
| 26 | + Run the `spacetime dev` command to create a new project with a SpacetimeDB module and Nuxt client. |
| 27 | + |
| 28 | + This will start the local SpacetimeDB server, publish your module, generate TypeScript bindings, and start the Nuxt development server. |
| 29 | + </StepText> |
| 30 | + <StepCode> |
| 31 | +```bash |
| 32 | +spacetime dev --template nuxt-ts |
| 33 | +``` |
| 34 | + </StepCode> |
| 35 | + </Step> |
| 36 | + |
| 37 | + <Step title="Open your app"> |
| 38 | + <StepText> |
| 39 | + Navigate to [http://localhost:5173](http://localhost:5173) to see your app running. |
| 40 | + |
| 41 | + The template includes a basic Nuxt app connected to SpacetimeDB. |
| 42 | + </StepText> |
| 43 | + </Step> |
| 44 | + |
| 45 | + <Step title="Explore the project structure"> |
| 46 | + <StepText> |
| 47 | + Your project contains both server and client code. |
| 48 | + |
| 49 | + Edit `spacetimedb/src/index.ts` to add tables and reducers. Edit `components/AppContent.vue` to build your UI, and `app.vue` to configure the SpacetimeDB connection. |
| 50 | + </StepText> |
| 51 | + <StepCode> |
| 52 | +``` |
| 53 | +my-spacetime-app/ |
| 54 | +├── spacetimedb/ # Your SpacetimeDB module |
| 55 | +│ └── src/ |
| 56 | +│ └── index.ts # SpacetimeDB module logic |
| 57 | +├── app.vue # Root component with provider |
| 58 | +├── components/ |
| 59 | +│ └── AppContent.vue # Main UI component |
| 60 | +├── server/ |
| 61 | +│ └── api/ |
| 62 | +│ └── people.get.ts # Server-side data fetching |
| 63 | +├── module_bindings/ # Auto-generated types |
| 64 | +├── nuxt.config.ts # Nuxt configuration |
| 65 | +└── package.json |
| 66 | +``` |
| 67 | + </StepCode> |
| 68 | + </Step> |
| 69 | + |
| 70 | + <Step title="Understand tables and reducers"> |
| 71 | + <StepText> |
| 72 | + Open `spacetimedb/src/index.ts` to see the module code. The template includes a `person` table and two reducers: `add` to insert a person, and `say_hello` to greet everyone. |
| 73 | + |
| 74 | + Tables store your data. Reducers are functions that modify data — they're the only way to write to the database. |
| 75 | + </StepText> |
| 76 | + <StepCode> |
| 77 | +```typescript |
| 78 | +import { schema, table, t } from 'spacetimedb/server'; |
| 79 | + |
| 80 | +export const spacetimedb = schema( |
| 81 | + table( |
| 82 | + { name: 'person', public: true }, |
| 83 | + { |
| 84 | + name: t.string(), |
| 85 | + } |
| 86 | + ) |
| 87 | +); |
| 88 | + |
| 89 | +spacetimedb.reducer('add', { name: t.string() }, (ctx, { name }) => { |
| 90 | + ctx.db.person.insert({ name }); |
| 91 | +}); |
| 92 | + |
| 93 | +spacetimedb.reducer('say_hello', (ctx) => { |
| 94 | + for (const person of ctx.db.person.iter()) { |
| 95 | + console.info(`Hello, ${person.name}!`); |
| 96 | + } |
| 97 | + console.info('Hello, World!'); |
| 98 | +}); |
| 99 | +``` |
| 100 | + </StepCode> |
| 101 | + </Step> |
| 102 | + |
| 103 | + <Step title="Test with the CLI"> |
| 104 | + <StepText> |
| 105 | + Use the SpacetimeDB CLI to call reducers and query your data directly. |
| 106 | + </StepText> |
| 107 | + <StepCode> |
| 108 | +```bash |
| 109 | +# Call the add reducer to insert a person |
| 110 | +spacetime call <database-name> add Alice |
| 111 | + |
| 112 | +# Query the person table |
| 113 | +spacetime sql <database-name> "SELECT * FROM person" |
| 114 | + name |
| 115 | +--------- |
| 116 | + "Alice" |
| 117 | + |
| 118 | +# Call say_hello to greet everyone |
| 119 | +spacetime call <database-name> say_hello |
| 120 | + |
| 121 | +# View the module logs |
| 122 | +spacetime logs <database-name> |
| 123 | +2025-01-13T12:00:00.000000Z INFO: Hello, Alice! |
| 124 | +2025-01-13T12:00:00.000000Z INFO: Hello, World! |
| 125 | +``` |
| 126 | + </StepCode> |
| 127 | + </Step> |
| 128 | + |
| 129 | + <Step title="Understand server-side rendering"> |
| 130 | + <StepText> |
| 131 | + The SpacetimeDB SDK works both server-side and client-side. The template uses a hybrid approach: |
| 132 | + |
| 133 | + - **Server API route** (`server/api/people.get.ts`): Fetches initial data during SSR for fast page loads |
| 134 | + - **Client composables**: Maintain a real-time WebSocket connection for live updates |
| 135 | + |
| 136 | + The server API route connects to SpacetimeDB, subscribes, fetches data, and disconnects. |
| 137 | + </StepText> |
| 138 | + <StepCode> |
| 139 | +```typescript |
| 140 | +// server/api/people.get.ts |
| 141 | +import { DbConnection } from '../../module_bindings'; |
| 142 | + |
| 143 | +export default defineEventHandler(async () => { |
| 144 | + return new Promise((resolve, reject) => { |
| 145 | + DbConnection.builder() |
| 146 | + .withUri(process.env.SPACETIMEDB_HOST!) |
| 147 | + .withModuleName(process.env.SPACETIMEDB_DB_NAME!) |
| 148 | + .onConnect((conn) => { |
| 149 | + conn.subscriptionBuilder() |
| 150 | + .onApplied(() => { |
| 151 | + const people = Array.from(conn.db.person.iter()); |
| 152 | + conn.disconnect(); |
| 153 | + resolve(people); |
| 154 | + }) |
| 155 | + .subscribe('SELECT * FROM person'); |
| 156 | + }) |
| 157 | + .build(); |
| 158 | + }); |
| 159 | +}); |
| 160 | +``` |
| 161 | + </StepCode> |
| 162 | + </Step> |
| 163 | + |
| 164 | + <Step title="Set up the SpacetimeDB provider"> |
| 165 | + <StepText> |
| 166 | + The root `app.vue` wraps your app in a `SpacetimeDBProvider` that manages the WebSocket connection. The provider is wrapped in `ClientOnly` so it only runs in the browser, while SSR uses the server API route for initial data. |
| 167 | + </StepText> |
| 168 | + <StepCode> |
| 169 | +```vue |
| 170 | +<!-- app.vue --> |
| 171 | +<template> |
| 172 | + <ClientOnly> |
| 173 | + <SpacetimeDBProvider :connection-builder="connectionBuilder"> |
| 174 | + <AppContent /> |
| 175 | + </SpacetimeDBProvider> |
| 176 | + <template #fallback> |
| 177 | + <AppContent /> |
| 178 | + </template> |
| 179 | + </ClientOnly> |
| 180 | +</template> |
| 181 | +
|
| 182 | +<script setup lang="ts"> |
| 183 | +import { SpacetimeDBProvider } from 'spacetimedb/vue'; |
| 184 | +import { DbConnection } from './module_bindings'; |
| 185 | +
|
| 186 | +const HOST = import.meta.env.VITE_SPACETIMEDB_HOST ?? 'ws://localhost:3000'; |
| 187 | +const DB_NAME = import.meta.env.VITE_SPACETIMEDB_DB_NAME ?? 'nuxt-ts'; |
| 188 | +
|
| 189 | +const connectionBuilder = import.meta.client |
| 190 | + ? DbConnection.builder() |
| 191 | + .withUri(HOST) |
| 192 | + .withModuleName(DB_NAME) |
| 193 | + .withToken(localStorage.getItem('auth_token') || undefined) |
| 194 | + .onConnect((_conn, identity, token) => { |
| 195 | + localStorage.setItem('auth_token', token); |
| 196 | + console.log('Connected:', identity.toHexString()); |
| 197 | + }) |
| 198 | + .onDisconnect(() => console.log('Disconnected')) |
| 199 | + .onConnectError((_ctx, err) => console.log('Error:', err)) |
| 200 | + : undefined; |
| 201 | +</script> |
| 202 | +``` |
| 203 | + </StepCode> |
| 204 | + </Step> |
| 205 | + |
| 206 | + <Step title="Use composables and SSR data together"> |
| 207 | + <StepText> |
| 208 | + Use `useFetch` to load initial data server-side, then Vue composables for real-time updates on the client. The component displays server-fetched data immediately while the WebSocket connection establishes. |
| 209 | + </StepText> |
| 210 | + <StepCode> |
| 211 | +```vue |
| 212 | +<!-- components/AppContent.vue --> |
| 213 | +<script setup lang="ts"> |
| 214 | +import { ref, computed } from 'vue'; |
| 215 | +import { tables, reducers } from '../module_bindings'; |
| 216 | +
|
| 217 | +// Fetch initial data server-side for SSR |
| 218 | +const { data: initialPeople } = await useFetch('/api/people'); |
| 219 | +
|
| 220 | +// On the client, use real-time composables |
| 221 | +let conn, people, addReducer; |
| 222 | +if (import.meta.client) { |
| 223 | + const { useSpacetimeDB, useTable, useReducer } = await import('spacetimedb/vue'); |
| 224 | + conn = useSpacetimeDB(); |
| 225 | + [people] = useTable(tables.person); |
| 226 | + addReducer = useReducer(reducers.add); |
| 227 | +} |
| 228 | +
|
| 229 | +// Use real-time data once connected, fall back to SSR data |
| 230 | +const displayPeople = computed(() => { |
| 231 | + if (conn?.isActive && people?.value) return people.value; |
| 232 | + return initialPeople.value ?? []; |
| 233 | +}); |
| 234 | +</script> |
| 235 | +``` |
| 236 | + </StepCode> |
| 237 | + </Step> |
| 238 | +</StepByStep> |
| 239 | + |
| 240 | +## Next steps |
| 241 | + |
| 242 | +- Read the [TypeScript SDK Reference](/sdks/typescript) for detailed API docs |
0 commit comments