How It Works
Brainbox’s collaboration system operates on four core principles:Local-first storage
Every client maintains a full SQLite database. All changes write locally first, ensuring instant responsiveness and offline capability.
Background sync
When connected, changes automatically sync to the server via WebSocket. The server persists data to PostgreSQL as the source of truth.
CRDT merge
Concurrent edits from multiple users merge automatically using Yjs CRDTs. No manual conflict resolution required.
Architecture Overview
The sync architecture consists of multiple layers:Synchronization Types
Brainbox syncs different types of data through specialized synchronizers:Node Updates
Changes to node attributes (name, fields, etc.)
Document Updates
Rich text content changes in pages and records
Collaborations
User presence and active collaborators
Node Reactions
Emoji reactions on messages
Node Interactions
User interactions and activity
Node Tombstones
Deleted nodes and cleanup
User Updates
User profile and settings changes
Real-Time Editing
Multiple users can edit the same document simultaneously without conflicts:Presence Awareness
See who else is viewing or editing:- Active users - Avatar indicators show who’s in the document
- Live cursors - See where others are typing in real-time
- Selections - View other users’ text selections
- Activity status - Online/offline indicators
CRDT-Based Merging
Brainbox uses Yjs for conflict-free collaborative editing:- How CRDTs Work
- Rich Text Handling
- Database Merging
CRDTs (Conflict-free Replicated Data Types) are data structures that can be updated independently on multiple replicas and automatically merged:Key properties:
- Commutative - Order of operations doesn’t matter
- Associative - Grouping of operations doesn’t matter
- Idempotent - Applying an operation twice has the same effect as once
Offline Mode
Brainbox works fully offline with automatic sync when reconnected:Offline Capabilities
Full CRUD
Create, read, update, and delete nodes offline
File Uploads
Queue file uploads for processing when online
Search
Search locally cached content
Optimistic UI
Changes appear instantly in the UI
Offline Workflow
Handling Conflicts
In most cases, conflicts resolve automatically via CRDTs. Manual intervention is rarely needed.
- Automatic Resolution
- Edge Cases
Most conflicts resolve without user action:Text edits:
- Concurrent insertions: Both apply, merged by CRDT
- Concurrent deletions: Deleted content removed
- Format conflicts: Both formats apply (text can be bold AND italic)
- Different fields: Both changes apply
- Same field: Last write wins (by timestamp)
- Record deletion: Tombstone propagates, record removed for all users
Sync Engine Implementation
The sync engine coordinates data flow between client and server:Client-Side Handlers
Located inpackages/client/src/handlers/:
Server-Side Broadcasters
Located inapps/server/src/synchronizers/:
Performance Optimization
Brainbox optimizes sync performance through:Batching
Multiple changes batch into single sync messages
Debouncing
Rapid edits debounce before syncing
Compression
WebSocket messages use compression
Delta Sync
Only changes sync, not full documents
Indexing
SQLite indexes speed up queries
Connection Pooling
Reuse database connections
Sync Strategies
- Immediate Sync
- Debounced Sync
- Batched Sync
High-priority changes sync immediately:
- Chat messages
- Node creation/deletion
- Permission changes
- User presence updates
Data Consistency
Brainbox ensures data consistency across clients:Consistency Guarantees
Eventual Consistency
Eventual Consistency
All clients eventually converge to the same state:
- Guarantee: Given no new updates, all replicas converge
- Timeline: Typically within seconds of reconnection
- Verification: Vector clocks track causal ordering
Causal Consistency
Causal Consistency
Operations preserve cause-and-effect relationships:
- Guarantee: If operation A causes operation B, all clients see A before B
- Implementation: Logical timestamps and dependency tracking
- Example: Reply to message always appears after original message
Strong Consistency (Server)
Strong Consistency (Server)
Server database maintains strong consistency:
- Guarantee: Server always has authoritative state
- Implementation: PostgreSQL ACID transactions
- Use: Conflict resolution and audit trail
Conflict-Free Operations
Certain operations are guaranteed conflict-free:| Operation | Conflict-Free? | Reason |
|---|---|---|
| Insert text | ✓ | CRDT positions are unique |
| Delete text | ✓ | Tombstones prevent resurrection |
| Create node | ✓ | UUIDs prevent ID collision |
| Update different fields | ✓ | Independent field state |
| Add reaction | ✓ | Set-based CRDT |
| Update same field | ✗ | Last-write-wins |
| Reorder items | ~ | Fractional indexing reduces conflicts |
Scaling Collaboration
Brainbox scales to support large teams:WebSocket Rooms
Users join workspace-specific rooms for targeted broadcasts
Redis Pub/Sub
Horizontal scaling across multiple server instances
Connection Pooling
Efficient database connection management
Rate Limiting
Prevent abuse with per-user rate limits
Best Practices
Trust the sync engine
Trust the sync engine
Don’t manually refresh to see changes. The sync engine automatically propagates updates in real-time.
Work offline confidently
Work offline confidently
Brainbox is designed for offline work. Don’t wait for internet connection - keep working and changes will sync later.
Avoid rapid field type changes
Avoid rapid field type changes
Changing field types (e.g., text to number) can cause data loss if users have offline edits. Plan schema changes carefully.
Monitor sync status
Monitor sync status
Watch the sync indicator in the UI. If changes aren’t syncing, check your connection.
Troubleshooting
- Sync Issues
- Conflict Issues
- Offline Issues
Changes not syncing:
- Check internet connection
- Verify WebSocket status in network tab
- Check browser console for errors
- Try disconnecting and reconnecting
- Clear local cache and re-sync
- Check network speed
- Reduce concurrent edits
- Limit file upload size
- Check server load
Technical Deep Dive
For developers interested in the implementation:CRDT Package
packages/crdt/ - Yjs document handlingClient Sync
packages/client/ - Sync engine and handlersServer Sync
apps/server/src/synchronizers/ - BroadcastersWebSocket API
apps/server/src/api/ - WebSocket routesNext Steps
Pages
Real-time collaborative editing
Databases
Collaborative data management
Chat
Real-time messaging
Development
Build with Brainbox