Expand description
Bevy integration for SpacetimeDB.
bevy_stdb adapts SpacetimeDB’s connection and callback model into
Bevy-style resources, systems, and messages. Everything is configured
through StdbPlugin.
§Architecture
The crate is organized around four lifecycle concerns:
- Connection — Establish the initial connection eagerly or on demand,
expose the active connection as
StdbConnection, and track state viaStdbConnectionState. - Tables — Register message channels once at startup and re-bind SDK
table callbacks whenever a connection becomes active. Row changes are
forwarded as Bevy
Messages (InsertMessage,DeleteMessage,UpdateMessage,InsertUpdateMessage). - Subscriptions — Store subscription intent separately from the live
connection via
StdbSubscriptionsso queries are automatically re-applied after reconnects. - Reconnect — Optionally retry failed connections with configurable
backoff via
StdbReconnectOptions.
§Quick start
ⓘ
use bevy::prelude::*;
use bevy_stdb::prelude::*;
// Generated by `spacetime generate`:
use module_bindings::{DbConnection, RemoteModule, PlayerRow};
#[derive(Clone, Eq, PartialEq, Hash)]
enum SubKey { Players }
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(
StdbPlugin::<DbConnection, RemoteModule>::default()
.with_module_name("my_module")
.with_uri("http://localhost:3000")
.with_background_driver(DbConnection::run_threaded)
.with_reconnect(StdbReconnectOptions::default())
.with_subscriptions(|subs: &mut StdbSubscriptions<SubKey, RemoteModule>| {
subs.subscribe_sql(SubKey::Players, "SELECT * FROM player");
})
.add_table::<PlayerRow>(|reg, db| reg.bind(db.player()))
)
.add_systems(Update, on_player_insert)
.run();
}
fn on_player_insert(mut msgs: ReadInsertMessage<PlayerRow>) {
for msg in msgs.read() {
info!("player inserted: {:?}", msg.row);
}
}See the StdbPlugin docs for the full
builder API and the README
for detailed guides on connection driving, table registration,
subscriptions, and delayed connections.
Modules§
- prelude
- Common imports for
bevy_stdb.