Skip to main content
Reference engineering Archived 35 min read Updated

docs-quickstarts-browser

\ Browser Quickstart | SpacetimeDB docs \ \document.documentElement.setAttribute("data-theme","light"),document.documentElement.setAttribute("data-theme-choice","light"),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()
\Version: 2.0.0
\\Browser Quickstart

Get a SpacetimeDB app running in the browser with inline JavaScript.

Prerequisites​

\
  • Node.js 18+ installed
  • SpacetimeDB CLI installed
  • \
    Create your project

    Run the spacetime dev command to create a new project with a TypeScript SpacetimeDB module.

    This will start the local SpacetimeDB server, publish your module, and generate TypeScript client bindings.

    spacetime dev --template browser-ts
    Build the client bindings

    The generated TypeScript bindings need to be bundled into a JavaScript file that can be loaded in the browser via a script tag.

    cd my-spacetime-app
    npm install
    npm run build
    Open in browser

    Open index.html directly in your browser. The app connects to SpacetimeDB and displays data in real-time.

    The JavaScript code runs inline in a script tag, using the bundled DbConnection class.

    tip

    The browser IIFE bundle also exposes the generated tables query builders, so you can use query-builder subscriptions here too.

    warning

    With no token, SpacetimeDB issues a server-issued identity and a non-expiring token; persist it and pass it back on reconnect to keep the same identity. A lost token can't be recovered, so self-issued identities are for development. For production, authenticate with an OIDC provider such as SpacetimeAuth, which handles token lifecycle. See Authentication.


    <script src="dist/bindings.iife.js">script>

    <script>
    const HOST = 'ws://localhost:3000';
    const DB_NAME = 'my-spacetime-app';
    const TOKEN_KEY = `${HOST}/${DB_NAME}/auth_token`;

    const conn = DbConnection.builder()
    .withUri(HOST)
    .withDatabaseName(DB_NAME)
    .withToken(localStorage.getItem(TOKEN_KEY))
    .onConnect((conn, identity, token) => {
    localStorage.setItem(TOKEN_KEY, token);
    console.log('Connected:', identity.toHexString());

    // Subscribe to tables
    conn.subscriptionBuilder()
    .onApplied(() => {
    for (const person of conn.db.person.iter()) {
    console.log(person.name);
    }
    })
    .subscribe(tables.person);
    })
    .build();
    script>
    Call reducers

    Reducers are functions that modify data — they're the only way to write to the database.

    // Call a reducer with named arguments
    conn.reducers.add({ name: 'Alice' }).catch(console.error);
    React to changes

    Register callbacks to update your UI when data changes.

    conn.db.person.onInsert((ctx, person) => {
    console.log('New person:', person.name);
    });

    conn.db.person.onDelete((ctx, person) => {
    console.log('Removed:', person.name);
    });

    Next steps​

    \
  • See the Chat App Tutorial for a complete example
  • Read the TypeScript SDK Reference for detailed API docs