mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-05-11 18:36:15 -04:00
6382220fa0
The basic-rs and basic-cpp templates say "Press any key to exit..." but stdin is line-buffered in cooked mode, so `read(&mut [0u8])` actually blocks until Enter is pressed. Adding a real "any key" handler would require a terminal crate dependency (crossterm, termion, etc.) which is heavy for a starter template. Instead, change the message to "Press Enter to exit..." and use `read_line()` to match the actual behavior. Also removes the unused `Read` import. Co-authored-by: clockwork-labs-bot <clockwork-labs-bot@users.noreply.github.com> Co-authored-by: Zeke Foppa <196249+bfops@users.noreply.github.com>
47 lines
1.5 KiB
Rust
47 lines
1.5 KiB
Rust
mod module_bindings;
|
|
use module_bindings::*;
|
|
use std::env;
|
|
|
|
use spacetimedb_sdk::{DbContext, Table};
|
|
|
|
fn main() {
|
|
// The URI of the SpacetimeDB instance hosting our chat module.
|
|
let host: String = env::var("SPACETIMEDB_HOST").unwrap_or("http://localhost:3000".to_string());
|
|
|
|
// The module name we chose when we published our module.
|
|
let db_name: String = env::var("SPACETIMEDB_DB_NAME").unwrap_or("my-db".to_string());
|
|
|
|
// Connect to the database
|
|
let conn = DbConnection::builder()
|
|
.with_database_name(db_name)
|
|
.with_uri(host)
|
|
.on_connect(|_, _, _| {
|
|
println!("Connected to SpacetimeDB");
|
|
})
|
|
.on_connect_error(|_ctx, e| {
|
|
eprintln!("Connection error: {:?}", e);
|
|
std::process::exit(1);
|
|
})
|
|
.build()
|
|
.expect("Failed to connect");
|
|
|
|
conn.run_threaded();
|
|
|
|
// Subscribe to the person table
|
|
conn.subscription_builder()
|
|
.on_applied(|_ctx| println!("Subscripted to the person table"))
|
|
.on_error(|_ctx, e| eprintln!("There was an error when subscring to the person table: {e}"))
|
|
.add_query(|q| q.from.person())
|
|
.subscribe();
|
|
|
|
// Register a callback for when rows are inserted into the person table
|
|
conn.db().person().on_insert(|_ctx, person| {
|
|
println!("New person: {}", person.name);
|
|
});
|
|
|
|
// Keep the main thread alive so the connection stays open
|
|
loop {
|
|
std::thread::sleep(std::time::Duration::from_secs(1));
|
|
}
|
|
}
|