Realtime subscriptions
Connect to PocketBase realtime, authorize subscriptions, process record events, unsubscribe, and recover a connection.
Realtime subscriptions deliver collection and record changes to connected clients. The server authorizes each subscription against the collection's list or view access rule, so a client must authenticate before subscribing to protected data.
Before you begin
- Run PocketBase at a reachable URL and have a collection such as
messages. - Configure the collection's API rules for the records the client should receive.
- Use a PocketBase SDK or a compatible realtime client; keep the client's auth token current.
Subscription lifecycle
Steps
Create a client pointed at your PocketBase server and authenticate the user before subscribing to protected records:
import PocketBase from "pocketbase";
const pb = new PocketBase("http://127.0.0.1:8090");
await pb.collection("users").authWithPassword(
"person@example.com",
"password-from-user",
);The client has a valid auth token. Public collection rules may allow an unauthenticated subscription, but protected rules require this step.
Subscribe to all events that the collection rule permits:
const unsubscribe = await pb.collection("messages").subscribe("*", (event) => {
console.log(event.action, event.record.id);
});The callback receives a record event. The action identifies the change, and the record contains the affected data that the request is authorized to see.
Use a record identifier when a screen needs updates for one entity:
const stopOrder = await pb.collection("orders").subscribe(
"ORDER_RECORD_ID",
(event) => console.log(event.action, event.record),
);The callback runs when the selected record changes and the current authorization permits the event.
Call the function returned by the subscription when the component or screen is disposed:
await unsubscribe();
await stopOrder();The client stops listening for those subscriptions. Do this before replacing a view to avoid stale callbacks and duplicate updates.
Handle the client's connection error or close signal by showing a reconnect state, refreshing authentication if its token is no longer valid, and establishing the subscription again after the connection returns.
async function reconnect() {
if (!pb.authStore.isValid) {
await pb.collection("users").authWithPassword(
"person@example.com",
"password-from-user",
);
}
return pb.collection("messages").subscribe("*", onMessage);
}Use backoff in production and remove the previous unsubscribe function before installing a replacement. Do not assume events missed while disconnected are replayed; refetch the relevant records after reconnecting and then resume the subscription.
Troubleshoot subscriptions
If connection succeeds but subscription authorization fails, inspect the collection's list or view API rule and the client's auth token. If no event arrives, confirm that the changed record matches the subscribed collection or record ID and that the server connection remains open. If duplicate callbacks appear after navigation, unsubscribe the old listener before reconnecting or mounting the view again. After recovery, refetch current records because realtime delivery is not a substitute for synchronization.
Next step
Continue with Realtime with client SDKs for SDK-specific subscription patterns.