Realtime
Subscribe to live database changes over WebSocket. Your app receives events instantly when records are created, updated, or deleted.
How it works
Koolbase Realtime uses a persistent WebSocket connection to the server. When any record in a subscribed collection is mutated, the server broadcasts the event to all connected clients subscribed to that collection.
Subscriptions are reference-counted — multiple subscribers on the same collection share one connection. The connection is automatically managed by the SDK.
Subscribe to a collection
Listen for records created, updated and deleted in a collection:
final created = Koolbase.realtime
.onRecordCreated(collection: 'posts')
.listen((record) => print('New post: ${record['title']}'));
final updated = Koolbase.realtime
.onRecordUpdated(collection: 'posts')
.listen((record) => print('Post updated: ${record['$id']}'));
final deleted = Koolbase.realtime
.onRecordDeleted(collection: 'posts')
.listen((recordId) => print('Post deleted: $recordId'));In Flutter, Koolbase.realtime.on(collection:) gives every event on one stream instead.
Unsubscribe
Stop listening when the screen goes away. When nothing listens to a collection any more, the SDK unsubscribes it, and closes the connection once nothing is subscribed at all.
class _PostsScreenState extends State<PostsScreen> {
StreamSubscription<Map<String, dynamic>>? _created;
@override
void initState() {
super.initState();
_created = Koolbase.realtime
.onRecordCreated(collection: 'posts')
.listen((record) => setState(() => posts.insert(0, record)));
}
@override
void dispose() {
_created?.cancel();
super.dispose();
}
}Events
createdthe recordA new record was inserted into the collectionupdatedthe recordAn existing record was updated — the full updated recorddeletedthe record idA record was deleted — only its idConnection
The SDK reconnects by itself when the connection drops and subscribes again to every collection you are listening to — there is nothing to re-subscribe. In Flutter, Koolbase.realtime.connectionState is a stream of true and false if you want to show it.
Who receives events
Realtime connects as the signed-in person, and reconnects as the new one when someone signs in or out — nothing to pass. Signed out, only a collection anyone can read delivers events. The collection's read rule decides the rest:
Owner-rule collections
owner read rule, events are filtered on the server — you only receive events for records the signed-in person created.