Realtime with client SDKs
Subscribe to PocketBase collection changes from JavaScript and Dart applications and clean up subscriptions safely.
Use a client SDK subscription when an application must react to record changes without polling. This guide covers the JavaScript and Dart SDK targets listed by PocketBase: create a client, subscribe, handle events, and unsubscribe when the owner is disposed.
The result is a listener that reports permitted changes to one collection and is removed when its owning screen or service ends. Keep the subscription lifecycle with that owner so a reopened screen does not create duplicate callbacks.
Before you begin
Install the JavaScript or Dart SDK and run PocketBase locally or use an existing server. Use a collection named messages; its API rules must permit the reader to view records.
Subscribe from JavaScript
import PocketBase from 'pocketbase';
const pb = new PocketBase('http://127.0.0.1:8090');The client is ready for requests.
const unsubscribe = await pb.collection('messages').subscribe('*', (event) => {
console.log(event.action, event.record.id);
});The callback receives the change action and current record. Keep the cleanup function with the component or service that created it.
await unsubscribe();
// Or remove all listeners for this collection:
await pb.collection('messages').unsubscribe();The client stops delivering events to the removed callback.
Subscribe from Dart
import 'package:pocketbase/pocketbase.dart';
final pb = PocketBase('http://127.0.0.1:8090');
await pb.collection('messages').subscribe('*', (event) {
print('${event.action} ${event.record.id}');
});
await pb.collection('messages').unsubscribe();Tie unsubscribe() to the lifecycle that created the subscription so reopened screens do not accumulate listeners.
Verify and handle failures
Create, update, or delete a permitted messages record from another client. The callback should receive the corresponding action and record identifier. If no event arrives, confirm the server URL, collection name, API rule, and active SDK connection. Query separately when you need durable confirmation that a write succeeded.
If the callback does not run, verify the server URL and collection name, then check that the active client can view messages. If it runs more than once for one change, inspect the owning lifecycle for an earlier subscription and call its saved cleanup function. If the write itself is uncertain, query the collection separately.
Next steps
See Realtime subscriptions, JavaScript SDK, and Dart SDK.