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

Function main

templates/basic-cpp/src/main.rs:7–46  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5use spacetimedb_sdk::{DbContext, Table};
6
7fn main() {
8 // The URI of the SpacetimeDB instance hosting our chat module.
9 let host: String = env::var("SPACETIMEDB_HOST").unwrap_or("http://localhost:3000".to_string());
10
11 // The module name we chose when we published our module.
12 let db_name: String = env::var("SPACETIMEDB_DB_NAME").unwrap_or("my-db".to_string());
13
14 // Connect to the database
15 let conn = DbConnection::builder()
16 .with_database_name(db_name)
17 .with_uri(host)
18 .on_connect(|_, _, _| {
19 println!("Connected to SpacetimeDB");
20 })
21 .on_connect_error(|_ctx, e| {
22 eprintln!("Connection error: {:?}", e);
23 std::process::exit(1);
24 })
25 .build()
26 .expect("Failed to connect");
27
28 conn.run_threaded();
29
30 // Subscribe to the person table
31 conn.subscription_builder()
32 .on_applied(|_ctx| println!("Subscripted to the person table"))
33 .on_error(|_ctx, e| eprintln!("There was an error when subscring to the person table: {e}"))
34 .add_query(|q| q.from.person())
35 .subscribe();
36
37 // Register a callback for when rows are inserted into the person table
38 conn.db().person().on_insert(|_ctx, person| {
39 println!("New person: {}", person.name);
40 });
41
42 // Keep the main thread alive so the connection stays open
43 loop {
44 std::thread::sleep(std::time::Duration::from_secs(1));
45 }
46}

Callers

nothing calls this directly

Calls 15

unwrap_orMethod · 0.80
on_connect_errorMethod · 0.80
on_connectMethod · 0.80
with_uriMethod · 0.80
with_database_nameMethod · 0.80
on_errorMethod · 0.80
on_appliedMethod · 0.80
to_stringMethod · 0.45
buildMethod · 0.45
run_threadedMethod · 0.45
subscribeMethod · 0.45
add_queryMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…