How Collaboration Works
AppFlowy Editor uses a transaction stream to broadcast changes. Each transaction represents a change to the document, which can be applied to other editor instances to keep them synchronized.Transaction Flow
- User makes an edit in Editor A
- Editor A creates a transaction
- Transaction is broadcast via
transactionStream - Editor B receives the transaction
- Editor B applies the transaction with
isRemote: true - Both editors are now synchronized
Basic Collaboration Setup
Here’s a simple example with two synchronized editors:Transaction Stream
ThetransactionStream emits events for every change:
Transaction Time
TransactionTime.before: Before the transaction is applied locallyTransactionTime.after: After the transaction is applied locally
TransactionTime.before to capture changes before they’re applied.
Remote Transactions
When applying transactions from other users, use theisRemote flag:
ApplyOptions
Network Integration
WebSocket Example
Here’s how to integrate with WebSocket for real-time collaboration:Conflict Resolution
For production collaborative editing, implement Operational Transformation (OT) or Conflict-free Replicated Data Types (CRDTs).Simple Last-Write-Wins
Selection Synchronization
Show other users’ cursors and selections:Presence Awareness
Track active users:Best Practices
- Use Remote Flag: Always set
isRemote: truefor transactions from other users - Transaction Timing: Listen to
TransactionTime.beforefor broadcasting changes - Debounce Selection: Don’t broadcast selection changes too frequently
- Handle Disconnections: Implement reconnection logic with state sync
- Version Control: Track document versions for conflict detection
- User Identification: Assign unique IDs and colors to each user
- Error Handling: Handle network errors and transaction failures gracefully
- Optimize Bandwidth: Send only necessary data, compress if needed
Performance Considerations
- Throttle Updates: Limit broadcast frequency for rapid typing
- Batch Operations: Group multiple small changes into single transactions
- Lazy Loading: Load document sections on demand for large documents
- Memory Management: Clean up disposed editor states and listeners
Security
- Authentication: Verify user identity before allowing edits
- Authorization: Check user permissions for document access
- Validation: Validate all incoming transactions
- Sanitization: Sanitize user input to prevent XSS attacks
- Rate Limiting: Prevent abuse with rate limits