Harmonics Trait
TheHarmonics trait is the foundation of Laravel Chorus. When added to your Eloquent models, it automatically tracks changes and enables real-time synchronization to connected clients.
Basic Usage
Adding the Trait
Add theHarmonics trait to any model you want to synchronize:
What the Trait Does
When you add theHarmonics trait, it automatically:
- Registers event listeners for model create, update, and delete operations
- Creates harmonic records in the harmonics table for each change
- Broadcasts changes via WebSocket to connected clients
- Provides synchronization methods for frontend integration
Defining Sync Fields
By default, no fields are synchronized for security. You must explicitly define which fields to sync using one of these methods:Method 1: Property-Based (Recommended)
Define sync fields using a protected property:Method 2: Method-Based
Define sync fields using a method for simple dynamic logic:Method 3: Dynamic Sync Fields
Override thegetSyncFields() method for complex dynamic logic:
Security First: Only include fields that are safe to expose to clients. Never sync sensitive information like passwords, API keys, or internal system data.
Field Selection Best Practices
Include These Fields
✅ Always include:- Primary key (
id) - User-visible data (
title,name,description) - Status/state fields (
status,is_active) - Relationships IDs (
user_id,category_id) - Timestamps (
created_at,updated_at)
Exclude These Fields
❌ Never include:- Passwords or authentication tokens
- Internal system fields (
internal_notes) - Sensitive personal information (
ssn,credit_card) - Large binary data (
file_content) - Admin-only fields (
admin_notes)
Example: User Model
Automatic Change Detection
The Harmonics trait automatically detects changes using Laravel’s model events:Create Operations
Update Operations
Delete Operations
Bulk Operations
Advanced Configuration
Custom Primary Keys
For models with non-standard primary keys:Testing Your Configuration
Manual Testing
Create a test record and verify harmonics are created:Debug Command
Use the built-in debug command to verify your configuration:- Currently connected channels
- Active user IDs
Common Issues and Solutions
No Harmonics Created
No Harmonics Created
Problem: Model changes don’t create harmonic records.Solutions:
- Verify the trait is added:
use Harmonics; - Ensure you’re using Eloquent methods (not raw queries)
- Check for exceptions in Laravel logs
Fields Not Syncing
Fields Not Syncing
Problem: Expected fields don’t appear in synchronized data.Solutions:
- Add fields to
$syncFieldsarray - Verify fields exist in database
- Check
harmonicsdatabase table for change event
Too Much Data Syncing
Too Much Data Syncing
Problem: Sync payload is too large or contains sensitive data.Solutions:
- Remove unnecessary fields from
$syncFields - Narrow down
syncFilterquery. - Implement proper filtering (covered in next section)
- Consider splitting large data into separate models
Performance Issues
Performance Issues
Problem: Model operations are slow after adding Harmonics trait.Solutions:
- Use database indexes on frequently queried sync fields
- Implement sync filtering to reduce harmonic volume
Next Steps
Now that your model has the Harmonics trait:Define Sync Fields
Learn advanced field selection and dynamic sync configuration
Apply Sync Filters
Control which records are synchronized to each user
Set Up Write Actions
Configure server-side write operations and validation
Frontend Integration
Connect your frontend to synchronized models
Your model is now ready for real-time synchronization! Continue with Sync Fields to learn advanced field configuration techniques.