MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / onConnect

Function onConnect

templates/nodejs-ts/src/main.ts:32–73  ·  view source on GitHub ↗
(
  conn: DbConnection,
  identity: Identity,
  token: string
)

Source from the content-addressed store, hash-verified

30}
31
32function onConnect(
33 conn: DbConnection,
34 identity: Identity,
35 token: string
36): void {
37 console.log('\nConnected to SpacetimeDB!');
38 console.log(`Identity: ${identity.toHexString().slice(0, 16)}...`);
39
40 // Save token for future connections
41 saveToken(token);
42
43 // Subscribe to all tables
44 conn
45 .subscriptionBuilder()
46 .onApplied(ctx => {
47 // Show current people
48 const people = [...ctx.db.person.iter()];
49 console.log(`\nCurrent people (${people.length}):`);
50 if (people.length === 0) {
51 console.log(' (none yet)');
52 } else {
53 for (const person of people) {
54 console.log(` - ${person.name}`);
55 }
56 }
57
58 console.log('\nPress Ctrl+C to exit');
59 })
60 .onError((_ctx, err) => {
61 console.error('Subscription error:', err);
62 })
63 .subscribeToAllTables();
64
65 // Register callbacks for table changes
66 conn.db.person.onInsert((_ctx: EventContext, person) => {
67 console.log(`[Added] ${person.name}`);
68 });
69
70 conn.db.person.onDelete((_ctx: EventContext, person) => {
71 console.log(`[Removed] ${person.name}`);
72 });
73}
74
75function onDisconnect(_ctx: ErrorContext, error?: Error): void {
76 if (error) {

Callers

nothing calls this directly

Calls 9

logMethod · 0.80
subscribeToAllTablesMethod · 0.80
onErrorMethod · 0.80
onAppliedMethod · 0.80
subscriptionBuilderMethod · 0.80
saveTokenFunction · 0.70
iterMethod · 0.65
toHexStringMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…