-
Notifications
You must be signed in to change notification settings - Fork 680
Quickstart remix #4113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bradleyshep
wants to merge
1
commit into
master
Choose a base branch
from
quickstart-remix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Quickstart remix #4113
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| --- | ||
| title: Remix Quickstart | ||
| sidebar_label: Remix | ||
| slug: /quickstarts/remix | ||
| hide_table_of_contents: true | ||
| --- | ||
|
|
||
| import { InstallCardLink } from "@site/src/components/InstallCardLink"; | ||
| import { StepByStep, Step, StepText, StepCode } from "@site/src/components/Steps"; | ||
|
|
||
|
|
||
| Get a SpacetimeDB Remix app running in under 5 minutes. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [Node.js](https://nodejs.org/) 18+ installed | ||
| - [SpacetimeDB CLI](https://spacetimedb.com/install) installed | ||
|
|
||
| <InstallCardLink /> | ||
|
|
||
| --- | ||
|
|
||
| <StepByStep> | ||
| <Step title="Create your project"> | ||
| <StepText> | ||
| Run the `spacetime dev` command to create a new project with a SpacetimeDB module and Remix client. | ||
|
|
||
| This will start the local SpacetimeDB server, publish your module, generate TypeScript bindings, and start the Remix development server. | ||
| </StepText> | ||
| <StepCode> | ||
| ```bash | ||
| spacetime dev --template remix-ts my-remix-app | ||
| ``` | ||
| </StepCode> | ||
| </Step> | ||
|
|
||
| <Step title="Open your app"> | ||
| <StepText> | ||
| Navigate to [http://localhost:3001](http://localhost:3001) to see your app running. | ||
|
|
||
| Note: The Remix dev server runs on port 3001 to avoid conflict with SpacetimeDB on port 3000. | ||
| </StepText> | ||
| </Step> | ||
|
|
||
| <Step title="Explore the project structure"> | ||
| <StepText> | ||
| Your project contains both server and client code using Remix with Vite. | ||
|
|
||
| Edit `spacetimedb/src/index.ts` to add tables and reducers. Edit `app/routes/_index.tsx` to build your UI. | ||
| </StepText> | ||
| <StepCode> | ||
| ``` | ||
| my-remix-app/ | ||
| ├── spacetimedb/ # Your SpacetimeDB module | ||
| │ └── src/ | ||
| │ └── index.ts # Server-side logic | ||
| ├── app/ # Remix app | ||
| │ ├── root.tsx # Root layout with providers | ||
| │ ├── providers.tsx # SpacetimeDB provider (client-only) | ||
| │ └── routes/ | ||
| │ └── _index.tsx # Home page | ||
| ├── src/ | ||
| │ └── module_bindings/ # Auto-generated types | ||
| └── package.json | ||
| ``` | ||
| </StepCode> | ||
| </Step> | ||
|
|
||
| <Step title="Understand tables and reducers"> | ||
| <StepText> | ||
| 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. | ||
|
|
||
| Tables store your data. Reducers are functions that modify data — they're the only way to write to the database. | ||
| </StepText> | ||
| <StepCode> | ||
| ```typescript | ||
| import { schema, table, t } from 'spacetimedb/server'; | ||
|
|
||
| export const spacetimedb = schema( | ||
| table( | ||
| { name: 'person', public: true }, | ||
| { | ||
| name: t.string(), | ||
| } | ||
| ) | ||
| ); | ||
|
|
||
| spacetimedb.reducer('add', { name: t.string() }, (ctx, { name }) => { | ||
| ctx.db.person.insert({ name }); | ||
| }); | ||
|
|
||
| spacetimedb.reducer('say_hello', (ctx) => { | ||
| for (const person of ctx.db.person.iter()) { | ||
| console.info(`Hello, ${person.name}!`); | ||
| } | ||
| console.info('Hello, World!'); | ||
| }); | ||
| ``` | ||
| </StepCode> | ||
| </Step> | ||
|
|
||
| <Step title="Test with the CLI"> | ||
| <StepText> | ||
| Use the SpacetimeDB CLI to call reducers and query your data directly. | ||
| </StepText> | ||
| <StepCode> | ||
| ```bash | ||
| # Call the add reducer to insert a person | ||
| spacetime call my-remix-app add Alice | ||
|
|
||
| # Query the person table | ||
| spacetime sql my-remix-app "SELECT * FROM person" | ||
| name | ||
| --------- | ||
| "Alice" | ||
|
|
||
| # Call say_hello to greet everyone | ||
| spacetime call my-remix-app say_hello | ||
|
|
||
| # View the module logs | ||
| spacetime logs my-remix-app | ||
| 2025-01-13T12:00:00.000000Z INFO: Hello, Alice! | ||
| 2025-01-13T12:00:00.000000Z INFO: Hello, World! | ||
| ``` | ||
| </StepCode> | ||
| </Step> | ||
|
|
||
| <Step title="Understand the provider pattern"> | ||
| <StepText> | ||
| SpacetimeDB is client-side only — it cannot run during server-side rendering. The `app/providers.tsx` file handles this by deferring the SpacetimeDB connection until the component mounts on the client. | ||
|
|
||
| The template uses environment variables for configuration. Set `VITE_SPACETIMEDB_HOST` and `VITE_SPACETIMEDB_DB_NAME` to override defaults. | ||
| </StepText> | ||
| <StepCode> | ||
| ```tsx | ||
| // app/providers.tsx | ||
| import { useMemo, useState, useEffect } from 'react'; | ||
| import { SpacetimeDBProvider } from 'spacetimedb/react'; | ||
| import { DbConnection } from '../src/module_bindings'; | ||
|
|
||
| const HOST = import.meta.env.VITE_SPACETIMEDB_HOST ?? 'ws://localhost:3000'; | ||
| const DB_NAME = import.meta.env.VITE_SPACETIMEDB_DB_NAME ?? 'my-remix-app'; | ||
|
|
||
| export function Providers({ children }: { children: React.ReactNode }) { | ||
| const [isClient, setIsClient] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| setIsClient(true); | ||
| }, []); | ||
|
|
||
| const connectionBuilder = useMemo(() => { | ||
| if (typeof window === 'undefined') return null; | ||
| return DbConnection.builder() | ||
| .withUri(HOST) | ||
| .withModuleName(DB_NAME); | ||
| }, []); | ||
|
|
||
| // During SSR, render children without provider | ||
| if (!isClient || !connectionBuilder) { | ||
| return <>{children}</>; | ||
| } | ||
|
|
||
| return ( | ||
| <SpacetimeDBProvider connectionBuilder={connectionBuilder}> | ||
| {children} | ||
| </SpacetimeDBProvider> | ||
| ); | ||
| } | ||
| ``` | ||
| </StepCode> | ||
| </Step> | ||
|
|
||
| <Step title="Use React hooks for data"> | ||
| <StepText> | ||
| In your route components, use `useTable` to subscribe to table data and `useReducer` to call reducers. The SpacetimeDB hooks work seamlessly with Remix routes. | ||
| </StepText> | ||
| <StepCode> | ||
| ```tsx | ||
| // app/routes/_index.tsx | ||
| import { tables, reducers } from '../../src/module_bindings'; | ||
| import { useTable, useReducer } from 'spacetimedb/react'; | ||
|
|
||
| export default function Index() { | ||
| // Subscribe to table data - returns [rows, isLoading] | ||
| const [people] = useTable(tables.person); | ||
|
|
||
| // Get a function to call a reducer | ||
| const addPerson = useReducer(reducers.add); | ||
|
|
||
| const handleAdd = () => { | ||
| // Call reducer with object syntax | ||
| addPerson({ name: 'Alice' }); | ||
| }; | ||
|
|
||
| return ( | ||
| <ul> | ||
| {people.map((person, i) => <li key={i}>{person.name}</li>)} | ||
| </ul> | ||
| ); | ||
| } | ||
| ``` | ||
| </StepCode> | ||
| </Step> | ||
| </StepByStep> | ||
|
|
||
| ## Next steps | ||
|
|
||
| - See the [Chat App Tutorial](/tutorials/chat-app) for a complete example | ||
| - Read the [TypeScript SDK Reference](/sdks/typescript) for detailed API docs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "description": "Remix with TypeScript server", | ||
| "client_lang": "typescript", | ||
| "server_lang": "typescript" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../licenses/apache2.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { useMemo, useState, useEffect } from 'react'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: I would just stuff the whole contents of this file in |
||
| import { SpacetimeDBProvider } from 'spacetimedb/react'; | ||
| import { DbConnection, ErrorContext } from '../src/module_bindings'; | ||
| import { Identity } from 'spacetimedb'; | ||
|
|
||
| const HOST = import.meta.env.VITE_SPACETIMEDB_HOST ?? 'ws://localhost:3000'; | ||
| const DB_NAME = import.meta.env.VITE_SPACETIMEDB_DB_NAME ?? 'remix-ts'; | ||
|
|
||
| const onConnect = (_conn: DbConnection, identity: Identity, token: string) => { | ||
| if (typeof window !== 'undefined') { | ||
| localStorage.setItem('auth_token', token); | ||
| } | ||
| console.log( | ||
| 'Connected to SpacetimeDB with identity:', | ||
| identity.toHexString() | ||
| ); | ||
| }; | ||
|
|
||
| const onDisconnect = () => { | ||
| console.log('Disconnected from SpacetimeDB'); | ||
| }; | ||
|
|
||
| const onConnectError = (_ctx: ErrorContext, err: Error) => { | ||
| console.log('Error connecting to SpacetimeDB:', err); | ||
| }; | ||
|
|
||
| export function Providers({ children }: { children: React.ReactNode }) { | ||
| const [isClient, setIsClient] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| setIsClient(true); | ||
| }, []); | ||
|
|
||
| const connectionBuilder = useMemo(() => { | ||
| if (typeof window === 'undefined') return null; | ||
| return DbConnection.builder() | ||
| .withUri(HOST) | ||
| .withModuleName(DB_NAME) | ||
| .withToken(localStorage.getItem('auth_token') || undefined) | ||
| .onConnect(onConnect) | ||
| .onDisconnect(onDisconnect) | ||
| .onConnectError(onConnectError); | ||
| }, []); | ||
|
|
||
| // During SSR or before hydration, render children without provider | ||
| if (!isClient || !connectionBuilder) { | ||
| return <>{children}</>; | ||
| } | ||
|
|
||
| return ( | ||
| <SpacetimeDBProvider connectionBuilder={connectionBuilder}> | ||
| {children} | ||
| </SpacetimeDBProvider> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { | ||
| Links, | ||
| Meta, | ||
| Outlet, | ||
| Scripts, | ||
| ScrollRestoration, | ||
| } from '@remix-run/react'; | ||
| import type { LinksFunction } from '@remix-run/node'; | ||
| import { Providers } from './providers'; | ||
|
|
||
| export const links: LinksFunction = () => []; | ||
|
|
||
| export function Layout({ children }: { children: React.ReactNode }) { | ||
| return ( | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charSet="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <Meta /> | ||
| <Links /> | ||
| </head> | ||
| <body> | ||
| <Providers>{children}</Providers> | ||
| <ScrollRestoration /> | ||
| <Scripts /> | ||
| </body> | ||
| </html> | ||
| ); | ||
| } | ||
|
|
||
| export default function App() { | ||
| return <Outlet />; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe this is correct (and if it is correct, I believe that's a bug). I think we should show that you can access SpacetimeDB state in the Loader if we can. We should at least try it out.
You should be able to access SpacetimeDB state both from the server and the client, although they will obviously each have to maintain their own connections.