How realtime works
The realtime system uses a publish-subscribe pattern:- Clients connect via SSE to
/api/realtime - Clients subscribe to specific collections or records
- Server broadcasts events when data changes
- Clients receive updates in real-time
Subscription topics
Clients can subscribe to different topics:Collection subscriptions
Record subscriptions
Wildcard subscriptions
Realtime events
Three types of events are broadcast:CREATE event
Triggered when a new record is created:UPDATE event
Triggered when a record is updated:DELETE event
Triggered when a record is deleted:Server-Side Events (SSE) connection
Realtime uses SSE for client-server communication:Connection lifecycle
Connection properties
Client identification
Each connection gets a unique client ID:Managing subscriptions
Setting subscriptions
Clients send a POST request to update their subscriptions:Each subscription request replaces all previous subscriptions for that client. To unsubscribe from everything, send an empty array.
Subscription validation
Subscription limits
- Maximum subscriptions per client: 1000
- Maximum subscription topic length: 2500 characters
- Client ID length: 1-255 characters
Authentication and authorization
Auth state management
Realtime respects authentication state:Auth upgrades
Clients can upgrade from guest to authenticated:API rules apply
Realtime events respect collection API rules:Server-side implementation
Broadcasting custom events
Sending to specific clients
Managing client state
Iterating over clients
Realtime hooks
Message hooks
Connection hooks
Subscription hooks
Connection management
Idle timeout
Connections timeout after inactivity:Connection limits
Implement connection limits:Graceful disconnection
Client libraries
PocketBase provides official client SDKs with realtime support:JavaScript/TypeScript
Dart/Flutter
Performance considerations
Limit subscription scope
Limit subscription scope
Subscribe only to the collections and records you need. Overly broad subscriptions waste bandwidth and processing power.
Batch updates when possible
Batch updates when possible
If you’re making multiple record changes, consider batching them to reduce the number of realtime events.
Implement client-side throttling
Implement client-side throttling
For high-frequency updates, throttle or debounce client-side handlers to avoid UI performance issues.
Monitor connection count
Monitor connection count
Track active realtime connections and implement limits to prevent resource exhaustion.
Use SSE compression
Use SSE compression
Enable HTTP compression on your reverse proxy to reduce bandwidth usage for realtime connections.
Clean up subscriptions
Clean up subscriptions
Always unsubscribe when components unmount or views change to prevent memory leaks.