Subscriptions
Subscriptions replicate database rows to your client in real-time. When you subscribe to a query, SpacetimeDB sends you the matching rows immediately and then pushes updates whenever those rows change.
Quick Start
Here's a complete example showing how to subscribe to data and react to changes:
- TypeScript
- C#
- Rust
- Unreal C++
- Unreal Blueprint
import { DbConnection, tables } from './module_bindings';
// Connect to the database
const conn = DbConnection.builder()
.withUri('wss://maincloud.spacetimedb.com')
.withDatabaseName('my_module')
.onConnect((ctx) => {
// Subscribe to users and messages using query builders
ctx.subscriptionBuilder()
.onApplied(() => {
console.log('Subscription ready!');
// Initial data is now in the client cache
for (const user of ctx.db.user.iter()) {
console.log(`User: ${user.name}`);
}
})
.subscribe([tables.user, tables.message]);
})
.build();
// React to new rows being inserted
conn.db.user.onInsert((ctx, user) => {
console.log(`New user joined: ${user.name}`);
});
// React to rows being deleted
conn.db.user.onDelete((ctx, user) => {
console.log(`User left: ${user.name}`);
});
// React to rows being updated
conn.db.user.onUpdate((ctx, oldUser, newUser) => {
console.log(`${oldUser.name} changed name to ${newUser.name}`);
});// Connect to the database
var conn = DbConnection.Builder()
.WithUri("wss://maincloud.spacetimedb.com")
.WithDatabaseName("my_module")
.OnConnect((ctx) =>
{
// Subscribe to users and messages
ctx.SubscriptionBuilder()
.OnApplied(() =>
{
Console.WriteLine("Subscription ready!");
// Initial data is now in the client cache
foreach (var user in ctx.Db.User.Iter())
{
Console.WriteLine($"User: {user.Name}");
}
})
.AddQuery(q => q.From.User())
.AddQuery(q => q.From.Message())
.Subscribe();
})
.Build();
// React to new rows being inserted
conn.Db.User.OnInsert += (ctx, user) =>
{
Console.WriteLine($"New user joined: {user.Name}");
};
// React to rows being deleted
conn.Db.User.OnDelete += (ctx, user) =>
{
Console.WriteLine($"User left: {user.Name}");
};
// React to rows being updated
conn.Db.User.OnUpdate += (ctx, oldUser, newUser) =>
{
Console.WriteLine($"{oldUser.Name} changed name to {newUser.Name}");
};// Connect to the database
let conn = DbConnection::builder()
.with_uri("wss://maincloud.spacetimedb.com")
.with_database_name("my_module")
.on_connect(|ctx| {
// Subscribe to users and messages
ctx.subscription_builder()
.on_applied(|ctx| {
println!("Subscription ready!");
// Initial data is now in the client cache
for user in ctx.db.user().iter() {
println!("User: {}", user.name);
}
})
.add_query(|q| q.from.user())
.add_query(|q| q.from.message())
.subscribe();
})
.build();
// React to new rows being inserted
conn.db().user().on_insert(|ctx, user| {
println!("New user joined: {}", user.name);
});
// React to rows being deleted
conn.db().user().on_delete(|ctx, user| {
println!("User left: {}", user.name);
});
// React to rows being updated
conn.db().user().on_update(|ctx, old_user, new_user| {
println!("{} changed name to {}", old_user.name, new_user.name);
});// Connect to the database
FOnConnectDelegate ConnectDelegate;
BIND_DELEGATE_SAFE(ConnectDelegate, this, AMyActor, OnConnected);
Conn = UDbConnection::Builder()
->WithUri(TEXT("wss://maincloud.spacetimedb.com"))
->WithDatabaseName(TEXT("my_module"))
->OnConnect(ConnectDelegate)
->Build();
// React to new rows being inserted
Conn->Db->User->OnInsert.AddDynamic(this, &AMyActor::OnUserInsert);
// React to rows being deleted
Conn->Db->User->OnDelete.AddDynamic(this, &AMyActor::OnUserDelete);
// React to rows being updated
Conn->Db->User->OnUpdate.AddDynamic(this, &AMyActor::OnUserUpdate);
void AMyActor::OnConnected(UDbConnection* Connection, FSpacetimeDBIdentity Identity, const FString& Token)
{
FOnSubscriptionApplied SubscriptionAppliedDelegate;
BIND_DELEGATE_SAFE(SubscriptionAppliedDelegate, this, AMyActor, OnSubscriptionApplied);
Connection->SubscriptionBuilder()
->OnApplied(SubscriptionAppliedDelegate)
->AddQuery([](const FQueryBuilder& Q)
{
return Q.From.User();
})
->AddQuery([](const FQueryBuilder& Q)
{
return Q.From.Message();
})
->Subscribe();
}
void AMyActor::OnSubscriptionApplied(const FSubscriptionEventContext& Context)
{
UE_LOG(LogTemp, Log, TEXT("Subscription ready!"));
// Initial data is now in the client cache
for (const FUserType& User : Context.Db->User->Iter())
{
UE_LOG(LogTemp, Log, TEXT("User: %s"), *User.Name);
}
}
Type-safe query builders are available in TypeScript, C#, Rust, and Unreal and are the recommended default. They provide auto-completion and compile-time type checking. For complete API details, see TypeScript, C#, Rust, and Unreal references.
How Subscriptions Work
\- Subscribe: Subscribe with queries to the data you need
- Receive initial data: SpacetimeDB sends all matching rows immediately
- Receive updates: When subscribed rows change, you get real-time updates
- React to changes: Use row callbacks (
onInsert,onDelete,onUpdate) to handle changes
The client maintains a local cache of subscribed data. Reading from the cache is instant since it's local memory.
For advanced raw SQL subscription syntax, see the SQL docs.
Common API Concepts
This page focuses on subscription behavior and usage patterns that apply across SDKs. For exact method signatures and SDK-specific overloads, use the language references.
Builder and Lifecycle Callbacks
All SDKs expose a builder API for creating subscriptions:
\- Register an applied callback: runs once initial matching rows are present in the local cache.
- Register an error callback: runs if subscription registration fails or a subscription later terminates with an error.
- Subscribe with one or more queries.
In Unreal, inspect the initial subscribed data set in OnApplied. Use table callbacks like OnInsert, OnDelete, and OnUpdate for later live changes.
Query Forms
All SDKs support subscriptions. Query builders are the recommended default across SDKs, with raw SQL available where needed:
| SDK | Typed Query Builder Support | Entry Point | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| TypeScript | Yes | tables. |


