Overview
The realtime system broadcasts events when records are created, updated, or deleted in collections. Clients can subscribe to specific collections and receive push notifications as changes occur.Key Benefits
- Instant Updates: No need to poll the server
- Reduced Bandwidth: Only receive relevant data changes
- Account Isolation: Events never cross account boundaries
- Flexible Subscriptions: Subscribe to specific collections and operations
- Dual Protocol Support: Choose WebSocket or SSE based on your needs
How It Works
Architecture
Event Flow
- A record is created, updated, or deleted via the REST API
- The record operation completes successfully
EventBroadcaster.publish_event()is called with the event details- The event is broadcast to all active connections in the same account
- Subscribers matching the collection and operation receive the event
Event Format
All realtime events follow this structure:- type:
{collection}.{operation}- The event type - timestamp: ISO 8601 timestamp of when the event occurred
- data: The full record data after the operation
Event Types
| Type | Description |
|---|---|
{collection}.create | A new record was created |
{collection}.update | An existing record was updated |
{collection}.delete | A record was deleted |
WebSocket vs SSE
Choose the protocol that fits your use case:WebSocket
Full-duplex communication with bidirectional messaging. Best for:- Interactive applications (chat, collaboration)
- Real-time games
- Applications that need to send messages to the server
- Lower latency
- Can send messages to server
- More control over connection
Server-Sent Events (SSE)
One-way communication from server to client over HTTP. Best for:- Simple notifications
- Live dashboards
- Feed updates
- Simpler implementation
- Automatic reconnection (handled by browser)
- Native browser support
Subscriptions
Subscribing to Collections
WebSocket:Operation Filtering
Subscribe to specific operations to reduce noise:Multiple Collections
Subscribe to multiple collections by creating multiple subscriptions:Authentication
Realtime connections require authentication via JWT access token.Authentication Methods
Via Query Parameter (recommended for WebSocket):Token Expiration
When your access token expires (after 1 hour), the connection will be closed. Use your refresh token to obtain a new access token and reconnect.Connection Management
Connection Limits
- Maximum 100 subscriptions per WebSocket connection
- Heartbeat sent every 30 seconds
- Connection closed on authentication failure
Reconnection Strategy
Always implement reconnection logic:Heartbeat Handling
Handle heartbeat messages to detect stale connections:Hook Integration
The realtime system integrates with SnackBase’s hook system.Realtime Hook Events
| Event | Description |
|---|---|
on_realtime_connect | Fired when a client connects |
on_realtime_disconnect | Fired when a client disconnects |
on_realtime_subscribe | Fired when a client subscribes to a collection |
on_realtime_unsubscribe | Fired when a client unsubscribes |
on_realtime_message | Fired when a message is received (WebSocket only) |
Example: Logging Realtime Events
Security Considerations
- Token Security: Always use HTTPS in production to protect tokens
- Account Isolation: Events never cross account boundaries
- Permission Validation: While realtime broadcasts to all subscribers, your application should validate permissions on the client side
- Token Expiration: Handle token expiration gracefully and reconnect with a new token
Best Practices
1. Filter Events on the Server
Use theoperations parameter to filter events server-side:
2. Use SSE for Simple Use Cases
If you only need to receive events, SSE is simpler:- Automatic reconnection handled by browser
- One-way communication (simpler API)
- Built-in heartbeat support