Harmonics
Harmonics are the fundamental units of change in Laravel Chorus. Every create, update, or delete operation on a synchronized model generates a harmonic record that captures what changed and broadcasts it to connected clients.What is a Harmonic?
A harmonic is a change record stored in theharmonics database table that contains:
- What changed - The table name and record ID
- How it changed - The operation type (create/update/delete)
- The new data - JSON payload of the synchronized fields
- Who can see it - Optional user_id for scoped synchronization (mainly used for rejected harmonics)
- When it happened - Timestamp for ordering and deduplication
The Harmonic Lifecycle
1. Change Detection
1
Change Detection
When a model with the
Harmonics trait is modified, Laravel’s model events automatically detect the change:2
Harmonic Creation
The trait’s event listeners create a harmonic record.
3
Broadcasting
The harmonic is immediately broadcast via WebSocket to connected clients.
4
Client Processing
Connected clients receive the harmonic and update their local IndexedDB.
Data Filtering in Harmonics
Field Filtering
Only fields defined in$syncFields are included in harmonic data:
Best Practices
Performance Optimization
Minimize Harmonic Data
Minimize Harmonic Data
- Only sync essential fields
- Use computed fields sparingly
- Avoid large text or binary data
- Consider separate models for large content
Efficient Filtering
Efficient Filtering
- Use database indexes for sync filters
- Avoid complex relationship queries in filters
- Cache expensive filter computations
- Use appropriate data types for filter fields
Batch Operations
Batch Operations
- Use bulk operations when possible
- Implement batch harmonic creation for imports
- Consider background processing for large datasets
- Use database transactions for consistency
Security Considerations
Field Security
Field Security
- Never sync sensitive data (passwords, tokens)
- Validate sync fields at runtime
- Use field whitelisting, not blacklisting
- Regular security audits of synced data
User Isolation
User Isolation
- Always implement sync filters for user data
- Validate user permissions in filters
- Use tenant-based isolation for multi-tenant apps
- Test authorization thoroughly
Data Validation
Data Validation
- Validate harmonic data before broadcasting
- Sanitize data for client consumption
- Implement server-side validation for all operations
- Use type checking for harmonic payloads
Authorization Policies
Authorization Policies
Next Steps
Write Path
Learn how client writes are processed and validated
Shadow Tables
Understand offline synchronization with shadow tables
React Integration
Use harmonics in your React components
Advanced Features
Explore advanced harmonic routing and channels
Harmonics are the foundation that makes Laravel Chorus’s real-time synchronization possible. Understanding how they work will help you design efficient, secure, and scalable real-time applications.