|
| 1 | +## SpacetimeDB Module Library and SDK |
| 2 | + |
| 3 | +### Overview |
| 4 | + |
| 5 | +This repository contains both the SpacetimeDB module library and the TypeScript SDK for SpacetimeDB. The SDK allows you to interact with the database server from a client and applies type information from your SpacetimeDB server module. |
| 6 | + |
| 7 | +### Installation |
| 8 | + |
| 9 | +The SDK is an NPM package, thus you can use your package manager of choice like NPM or Yarn, for example: |
| 10 | + |
| 11 | +``` |
| 12 | +npm add spacetimedb |
| 13 | +``` |
| 14 | + |
| 15 | +You can use the package in the browser, using a bundler like vite/parcel/rsbuild, in server-side applications like NodeJS, Deno, Bun, NextJS, Remix, and in Cloudflare Workers. |
| 16 | + |
| 17 | +> NOTE: For usage in NodeJS 18-21, you need to install the `undici` package as a peer dependency: `npm add spacetimedb undici`. Node 22 and later are supported out of the box. |
| 18 | +
|
| 19 | +### Usage |
| 20 | + |
| 21 | +In order to connect to a database you have to generate module bindings for your database. |
| 22 | + |
| 23 | +```ts |
| 24 | +import { DbConnection } from './module_bindings'; |
| 25 | + |
| 26 | +const connection = DbConnection.builder() |
| 27 | + .withUri('ws://localhost:3000') |
| 28 | + .withModuleName('MODULE_NAME') |
| 29 | + .onDisconnect(() => { |
| 30 | + console.log('disconnected'); |
| 31 | + }) |
| 32 | + .onConnectError(() => { |
| 33 | + console.log('client_error'); |
| 34 | + }) |
| 35 | + .onConnect((connection, identity, _token) => { |
| 36 | + console.log( |
| 37 | + 'Connected to SpacetimeDB with identity:', |
| 38 | + identity.toHexString() |
| 39 | + ); |
| 40 | + |
| 41 | + connection.subscriptionBuilder().subscribe('SELECT * FROM player'); |
| 42 | + }) |
| 43 | + .withToken('TOKEN') |
| 44 | + .build(); |
| 45 | +``` |
| 46 | + |
| 47 | +If you need to disconnect the client: |
| 48 | + |
| 49 | +```ts |
| 50 | +connection.disconnect(); |
| 51 | +``` |
| 52 | + |
| 53 | +Typically, you will use the SDK with types generated from SpacetimeDB module. For example, given a table named `Player` you can subscribe to player updates like this: |
| 54 | + |
| 55 | +```ts |
| 56 | +connection.db.player.onInsert((ctx, player) => { |
| 57 | + console.log(player); |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +Given a reducer called `CreatePlayer` you can call it using a call method: |
| 62 | + |
| 63 | +```ts |
| 64 | +connection.reducers.createPlayer(); |
| 65 | +``` |
| 66 | + |
| 67 | +#### React Usage |
| 68 | + |
| 69 | +This module also include React hooks to subscribe to tables under the `spacetimedb/react` subpath. In order to use SpacetimeDB React hooks in your project, first add a `SpacetimeDBProvider` at the top of your component hierarchy: |
| 70 | + |
| 71 | +```tsx |
| 72 | +const connectionBuilder = DbConnection.builder() |
| 73 | + .withUri('ws://localhost:3000') |
| 74 | + .withModuleName('MODULE_NAME') |
| 75 | + .withLightMode(true) |
| 76 | + .onDisconnect(() => { |
| 77 | + console.log('disconnected'); |
| 78 | + }) |
| 79 | + .onConnectError(() => { |
| 80 | + console.log('client_error'); |
| 81 | + }) |
| 82 | + .onConnect((conn, identity, _token) => { |
| 83 | + console.log( |
| 84 | + 'Connected to SpacetimeDB with identity:', |
| 85 | + identity.toHexString() |
| 86 | + ); |
| 87 | + |
| 88 | + conn.subscriptionBuilder().subscribe('SELECT * FROM player'); |
| 89 | + }) |
| 90 | + .withToken('TOKEN'); |
| 91 | + |
| 92 | +ReactDOM.createRoot(document.getElementById('root')!).render( |
| 93 | + <React.StrictMode> |
| 94 | + <SpacetimeDBProvider connectionBuilder={connectionBuilder}> |
| 95 | + <App /> |
| 96 | + </SpacetimeDBProvider> |
| 97 | + </React.StrictMode> |
| 98 | +); |
| 99 | +``` |
| 100 | + |
| 101 | +One you add a `SpacetimeDBProvider` to your hierarchy, you can use SpacetimeDB React hooks in your render function: |
| 102 | + |
| 103 | +```tsx |
| 104 | +function App() { |
| 105 | + const conn = useSpacetimeDB<DbConnection>(); |
| 106 | + const { rows: messages } = useTable<DbConnection, Message>('message'); |
| 107 | + |
| 108 | + ... |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +### Developer notes |
| 113 | + |
| 114 | +To run the tests, do: |
| 115 | + |
| 116 | +```sh |
| 117 | +pnpm build && pnpm test |
| 118 | +``` |
0 commit comments