Skip to content

Commit 9e7bce3

Browse files
Fixes the typescript quickstart for the new subscription API (#161)
* Updated quickstart url * DBConnection -> DbConnection for TypeScript SDK * Updated for the subscription update
1 parent 6b44934 commit 9e7bce3

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

docs/sdks/typescript/quickstart.md

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -384,27 +384,42 @@ module_bindings
384384
└── user_type.ts
385385
```
386386

387-
With `spacetime generate` we have generated TypeScript types derived from the types you specified in your module, which we can conveniently use in our client. We've placed these in the `module_bindings` folder. The main entry to the SpacetimeDB API is the `DBConnection`, a type which manages a connection to a remote database. Let's import it and a few other types into our `client/src/App.tsx`.
387+
With `spacetime generate` we have generated TypeScript types derived from the types you specified in your module, which we can conveniently use in our client. We've placed these in the `module_bindings` folder. The main entry to the SpacetimeDB API is the `DbConnection`, a type which manages a connection to a remote database. Let's import it and a few other types into our `client/src/App.tsx`.
388388

389389
```tsx
390-
import { DBConnection, EventContext, Message, User } from './module_bindings';
390+
import { DbConnection, EventContext, Message, User } from './module_bindings';
391391
import { Identity } from '@clockworklabs/spacetimedb-sdk';
392392
```
393393

394394
## Create your SpacetimeDB client
395395

396-
Now that we've imported the `DBConnection` type, we can use it to connect our app to our module.
396+
Now that we've imported the `DbConnection` type, we can use it to connect our app to our module.
397397

398398
Add the following to your `App` function, just below `const [newMessage, setNewMessage] = useState('');`:
399399

400400
```tsx
401401
const [connected, setConnected] = useState<boolean>(false);
402402
const [identity, setIdentity] = useState<Identity | null>(null);
403-
const [conn, setConn] = useState<DBConnection | null>(null);
403+
const [conn, setConn] = useState<DbConnection | null>(null);
404404

405405
useEffect(() => {
406+
const subscribeToQueries = (conn: DbConnection, queries: string[]) => {
407+
let count = 0;
408+
for (const query of queries) {
409+
conn
410+
?.subscriptionBuilder()
411+
.onApplied(() => {
412+
count++;
413+
if (count === queries.length) {
414+
console.log('SDK client cache initialized.');
415+
}
416+
})
417+
.subscribe(query);
418+
}
419+
};
420+
406421
const onConnect = (
407-
conn: DBConnection,
422+
conn: DbConnection,
408423
identity: Identity,
409424
token: string
410425
) => {
@@ -415,25 +430,24 @@ Add the following to your `App` function, just below `const [newMessage, setNewM
415430
'Connected to SpacetimeDB with identity:',
416431
identity.toHexString()
417432
);
418-
conn
419-
.subscriptionBuilder()
420-
.onApplied(() => {
421-
console.log('SDK client cache initialized.');
422-
})
423-
.subscribe(['SELECT * FROM message', 'SELECT * FROM user']);
433+
conn.reducers.onSendMessage(() => {
434+
console.log('Message sent.');
435+
});
436+
437+
subscribeToQueries(conn, ['SELECT * FROM message', 'SELECT * FROM user']);
424438
};
425439

426440
const onDisconnect = () => {
427441
console.log('Disconnected from SpacetimeDB');
428442
setConnected(false);
429443
};
430444

431-
const onConnectError = (_conn: DBConnection, err: Error) => {
445+
const onConnectError = (_conn: DbConnection, err: Error) => {
432446
console.log('Error connecting to SpacetimeDB:', err);
433447
};
434448

435449
setConn(
436-
DBConnection.builder()
450+
DbConnection.builder()
437451
.withUri('ws://localhost:3000')
438452
.withModuleName('quickstart-chat')
439453
.withToken(localStorage.getItem('auth_token') || '')
@@ -455,12 +469,12 @@ In the `onConnect` function we are also subscribing to the `message` and `user`
455469

456470
### Accessing the Data
457471

458-
Once SpacetimeDB is connected, we can easily access the data in the client cache using our `DBConnection`. The `conn.db` field allows you to access all of the tables of your database. Those tables will contain all data requested by your subscription configuration.
472+
Once SpacetimeDB is connected, we can easily access the data in the client cache using our `DbConnection`. The `conn.db` field allows you to access all of the tables of your database. Those tables will contain all data requested by your subscription configuration.
459473

460474
Let's create custom React hooks for the `message` and `user` tables. Add the following code above your `App` component:
461475

462476
```tsx
463-
function useMessages(conn: DBConnection | null): Message[] {
477+
function useMessages(conn: DbConnection | null): Message[] {
464478
const [messages, setMessages] = useState<Message[]>([]);
465479

466480
useEffect(() => {
@@ -491,7 +505,7 @@ function useMessages(conn: DBConnection | null): Message[] {
491505
return messages;
492506
}
493507

494-
function useUsers(conn: DBConnection | null): Map<string, User> {
508+
function useUsers(conn: DbConnection | null): Map<string, User> {
495509
const [users, setUsers] = useState<Map<string, User>>(new Map());
496510

497511
useEffect(() => {
@@ -648,7 +662,7 @@ Our `user` table includes all users not just online users, so we want to take ca
648662

649663
Here we post a message saying a new user has connected if the user is being added to the `user` table and they're online, or if an existing user's online status is being set to "online".
650664

651-
Note that `onInsert` and `onDelete` callbacks takes two arguments: an `EventContext` and the row. The `EventContext` can be used just like the `DBConnection` and has all the same access functions, in addition to containing information about the event that triggered this callback. For now, we can ignore this argument though, since we have all the info we need in the user rows.
665+
Note that `onInsert` and `onDelete` callbacks takes two arguments: an `EventContext` and the row. The `EventContext` can be used just like the `DbConnection` and has all the same access functions, in addition to containing information about the event that triggered this callback. For now, we can ignore this argument though, since we have all the info we need in the user rows.
652666

653667
## Conclusion
654668

0 commit comments

Comments
 (0)