|
| 1 | +--- |
| 2 | +title: Next.js Quickstart |
| 3 | +sidebar_label: Next.js |
| 4 | +slug: /quickstarts/nextjs |
| 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 Next.js 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 Next.js client. |
| 27 | + |
| 28 | + This will start the local SpacetimeDB server, publish your module, generate TypeScript bindings, and start the Next.js development server. |
| 29 | + </StepText> |
| 30 | + <StepCode> |
| 31 | +```bash |
| 32 | +spacetime dev --template nextjs-ts my-nextjs-app |
| 33 | +``` |
| 34 | + </StepCode> |
| 35 | + </Step> |
| 36 | + |
| 37 | + <Step title="Open your app"> |
| 38 | + <StepText> |
| 39 | + Navigate to [http://localhost:3000](http://localhost:3000) to see your app running. |
| 40 | + |
| 41 | + The `spacetime dev` command automatically configures your app to connect to SpacetimeDB via environment variables in `.env.local`. |
| 42 | + </StepText> |
| 43 | + </Step> |
| 44 | + |
| 45 | + <Step title="Explore the project structure"> |
| 46 | + <StepText> |
| 47 | + Your project contains both server and client code using the Next.js App Router. |
| 48 | + |
| 49 | + Edit `spacetimedb/src/index.ts` to add tables and reducers. Edit `app/page.tsx` and `app/PersonList.tsx` to build your UI. |
| 50 | + </StepText> |
| 51 | + <StepCode> |
| 52 | +``` |
| 53 | +my-nextjs-app/ |
| 54 | +├── spacetimedb/ # Your SpacetimeDB module |
| 55 | +│ └── src/ |
| 56 | +│ └── index.ts # SpacetimeDB module logic |
| 57 | +├── app/ # Next.js App Router |
| 58 | +│ ├── layout.tsx # Root layout with providers |
| 59 | +│ ├── page.tsx # Server Component (fetches initial data) |
| 60 | +│ ├── PersonList.tsx # Client Component (real-time updates) |
| 61 | +│ └── providers.tsx # SpacetimeDB provider for real-time |
| 62 | +├── lib/ |
| 63 | +│ └── spacetimedb-server.ts # Server-side data fetching |
| 64 | +├── src/ |
| 65 | +│ └── module_bindings/ # Auto-generated types |
| 66 | +└── package.json |
| 67 | +``` |
| 68 | + </StepCode> |
| 69 | + </Step> |
| 70 | + |
| 71 | + <Step title="Understand tables and reducers"> |
| 72 | + <StepText> |
| 73 | + 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. |
| 74 | + |
| 75 | + Tables store your data. Reducers are functions that modify data — they're the only way to write to the database. |
| 76 | + </StepText> |
| 77 | + <StepCode> |
| 78 | +```typescript |
| 79 | +import { schema, table, t } from 'spacetimedb/server'; |
| 80 | + |
| 81 | +export const spacetimedb = schema( |
| 82 | + table( |
| 83 | + { name: 'person', public: true }, |
| 84 | + { |
| 85 | + name: t.string(), |
| 86 | + } |
| 87 | + ) |
| 88 | +); |
| 89 | + |
| 90 | +spacetimedb.reducer('add', { name: t.string() }, (ctx, { name }) => { |
| 91 | + ctx.db.person.insert({ name }); |
| 92 | +}); |
| 93 | + |
| 94 | +spacetimedb.reducer('say_hello', (ctx) => { |
| 95 | + for (const person of ctx.db.person.iter()) { |
| 96 | + console.info(`Hello, ${person.name}!`); |
| 97 | + } |
| 98 | + console.info('Hello, World!'); |
| 99 | +}); |
| 100 | +``` |
| 101 | + </StepCode> |
| 102 | + </Step> |
| 103 | + |
| 104 | + <Step title="Test with the CLI"> |
| 105 | + <StepText> |
| 106 | + Use the SpacetimeDB CLI to call reducers and query your data directly. |
| 107 | + </StepText> |
| 108 | + <StepCode> |
| 109 | +```bash |
| 110 | +# Call the add reducer to insert a person |
| 111 | +spacetime call my-nextjs-app add Alice |
| 112 | + |
| 113 | +# Query the person table |
| 114 | +spacetime sql my-nextjs-app "SELECT * FROM person" |
| 115 | + name |
| 116 | +--------- |
| 117 | + "Alice" |
| 118 | + |
| 119 | +# Call say_hello to greet everyone |
| 120 | +spacetime call my-nextjs-app say_hello |
| 121 | + |
| 122 | +# View the module logs |
| 123 | +spacetime logs my-nextjs-app |
| 124 | +2025-01-13T12:00:00.000000Z INFO: Hello, Alice! |
| 125 | +2025-01-13T12:00:00.000000Z INFO: Hello, World! |
| 126 | +``` |
| 127 | + </StepCode> |
| 128 | + </Step> |
| 129 | + |
| 130 | + <Step title="Understand server-side rendering"> |
| 131 | + <StepText> |
| 132 | + The SpacetimeDB SDK works both server-side and client-side. The template uses a hybrid approach: |
| 133 | + |
| 134 | + - **Server Component** (`page.tsx`): Fetches initial data during SSR for fast page loads |
| 135 | + - **Client Component** (`PersonList.tsx`): Maintains a real-time WebSocket connection for live updates |
| 136 | + |
| 137 | + The `lib/spacetimedb-server.ts` file provides a utility for server-side data fetching. |
| 138 | + </StepText> |
| 139 | + <StepCode> |
| 140 | +```tsx |
| 141 | +// lib/spacetimedb-server.ts |
| 142 | +import { DbConnection } from '../src/module_bindings'; |
| 143 | + |
| 144 | +export async function fetchPeople() { |
| 145 | + return new Promise((resolve, reject) => { |
| 146 | + const connection = DbConnection.builder() |
| 147 | + .withUri(process.env.SPACETIMEDB_HOST!) |
| 148 | + .withModuleName(process.env.SPACETIMEDB_DB_NAME!) |
| 149 | + .onConnect(conn => { |
| 150 | + conn.subscriptionBuilder() |
| 151 | + .onApplied(() => { |
| 152 | + const people = Array.from(conn.db.person.iter()); |
| 153 | + conn.disconnect(); |
| 154 | + resolve(people); |
| 155 | + }) |
| 156 | + .subscribe('SELECT * FROM person'); |
| 157 | + }) |
| 158 | + .build(); |
| 159 | + }); |
| 160 | +} |
| 161 | +``` |
| 162 | + </StepCode> |
| 163 | + </Step> |
| 164 | + |
| 165 | + <Step title="Use React hooks for real-time data"> |
| 166 | + <StepText> |
| 167 | + In client components, use `useTable` to subscribe to table data and `useReducer` to call reducers. The Server Component passes initial data as props for instant rendering. |
| 168 | + </StepText> |
| 169 | + <StepCode> |
| 170 | +```tsx |
| 171 | +// app/page.tsx (Server Component) |
| 172 | +import { PersonList } from './PersonList'; |
| 173 | +import { fetchPeople } from '../lib/spacetimedb-server'; |
| 174 | + |
| 175 | +export default async function Home() { |
| 176 | + const initialPeople = await fetchPeople(); |
| 177 | + return <PersonList initialPeople={initialPeople} />; |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +```tsx |
| 182 | +// app/PersonList.tsx (Client Component) |
| 183 | +'use client'; |
| 184 | + |
| 185 | +import { tables, reducers } from '../src/module_bindings'; |
| 186 | +import { useTable, useReducer } from 'spacetimedb/react'; |
| 187 | + |
| 188 | +export function PersonList({ initialPeople }) { |
| 189 | + // Real-time data from WebSocket subscription |
| 190 | + const [people, isLoading] = useTable(tables.person); |
| 191 | + const addPerson = useReducer(reducers.add); |
| 192 | + |
| 193 | + // Use server data until client is connected |
| 194 | + const displayPeople = isLoading ? initialPeople : people; |
| 195 | + |
| 196 | + return ( |
| 197 | + <ul> |
| 198 | + {displayPeople.map((person, i) => <li key={i}>{person.name}</li>)} |
| 199 | + </ul> |
| 200 | + ); |
| 201 | +} |
| 202 | +``` |
| 203 | + </StepCode> |
| 204 | + </Step> |
| 205 | +</StepByStep> |
| 206 | + |
| 207 | +## Next steps |
| 208 | + |
| 209 | +- See the [Chat App Tutorial](/tutorials/chat-app) for a complete example |
| 210 | +- Read the [TypeScript SDK Reference](/sdks/typescript) for detailed API docs |
0 commit comments