Realtime API
Cocobase provides a powerful real-time engine built on WebSockets, allowing you to subscribe to data changes and build collaborative features like chat and live notifications.Overview
Key features:- Collection Watching: Subscribe to any collection to receive live updates.
- Broadcast Messaging: Send messages to all connected clients in a room.
- Filtering: Only receive events that match your specific criteria.
- Reconnection: Automatic handled by SDKs, manual for raw WebSockets.
- Platform Support: Works on Web, Mobile (Flutter), and Backend (Python/Go).
Connection
Connect to the Cocobase realtime server.- Endpoint:
wss://api.cocobase.buzz/realtime
1. Connect and Authenticate
After establishing the WebSocket connection, you must send an authentication message.2. Subscribe to Collection
Once authenticated, join a collection to start receiving events.Event Types
When a change occurs, you will receive an event message.| Event Type | Description |
|---|---|
created | A new document was added to the collection. |
updated | An existing document was modified. |
deleted | A document was removed. |
broadcast | A custom message sent to the room. |
Event Message Format
Broadcast & Rooms
Rooms allow you to isolate real-time communication for specific topics or entities (like a chat room).Join a Room
Send a Broadcast
Send a custom message to everyone in the same room.Example (JavaScript)
Reconnection Logic
When using the raw WebSocket API:- Exponential Backoff: Pulse connection attempts with increasing delays.
- State Resync: Re-subscribe to all previously watched collections upon reconnection.
- Heartbeat: Send periodic ping messages if the connection is idle to prevent timeout.
