Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Firebase Sync Laravel Package

mpociot/laravel-firebase-sync

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require mpociot/laravel-firebase-sync
    

    Add Firebase credentials to config/services.php:

    'firebase' => [
        'database_url' => env('FIREBASE_DATABASE_URL'),
        'secret' => env('FIREBASE_SECRET'),
    ]
    
  2. Publish Config:

    php artisan vendor:publish --provider="Mpociot\FirebaseSync\FirebaseSyncServiceProvider"
    

    Update .env with your Firebase credentials.

  3. First Use Case: Sync a model (e.g., Post) to Firebase:

    use Mpociot\FirebaseSync\HasFirebaseSync;
    
    class Post extends Model
    {
        use HasFirebaseSync;
    }
    

    Automatically syncs create, update, and delete operations to Firebase.


Implementation Patterns

Core Workflows

  1. Model Integration:

    • Add HasFirebaseSync trait to Eloquent models.
    • Customize sync behavior via firebaseSync() method:
      protected $firebaseSync = [
          'path' => 'posts', // Firebase path (default: plural model name)
          'fields' => ['title', 'content'], // Fields to sync (default: all fillable)
      ];
      
  2. Manual Sync:

    • Force sync a model instance:
      $post = Post::find(1);
      $post->syncToFirebase();
      
    • Listen for Firebase updates:
      Post::listenForFirebaseUpdates(function ($data) {
          // Handle realtime updates
      });
      
  3. Querying Firebase:

    • Fetch Firebase data directly:
      $posts = Post::getFromFirebase();
      
    • Query Firebase with conditions:
      $posts = Post::getFromFirebase(['limitToLast' => 5]);
      

Integration Tips

  • Authentication: Use Firebase Admin SDK for server-side auth (not covered by this package).
  • Validation: Combine with Laravel validation before syncing.
  • Events: Dispatch custom events after sync:
    $post->syncToFirebase()->fireSyncEvent();
    
  • Soft Deletes: Exclude soft-deleted models via firebaseSync():
    protected $firebaseSync = [
        'excludeDeleted' => true,
    ];
    

Gotchas and Tips

Pitfalls

  1. Outdated Package:

    • Last release in 2016—Firebase API may have changed. Test thoroughly.
    • Firebase Realtime Database is deprecated in favor of Firestore. Consider alternatives if using newer Firebase features.
  2. Configuration Quirks:

    • database_url must end with .firebaseio.com (not .com).
    • secret is optional but recommended for write operations.
  3. Data Mismatches:

    • Firebase stores data as JSON; ensure model attributes are JSON-serializable.
    • Avoid syncing large binary data (use Firebase Storage instead).
  4. Race Conditions:

    • Concurrent writes may overwrite data. Implement optimistic locking:
      protected $firebaseSync = [
          'optimisticLock' => 'updated_at',
      ];
      

Debugging

  • Enable Logging:

    'firebase' => [
        'log' => true, // Add to config/services.php
    ];
    

    Check storage/logs/laravel-firebase-sync.log.

  • Common Errors:

    • 401 Unauthorized: Verify secret or Firebase rules.
    • 403 Forbidden: Check Firebase Database rules (allow read/write for testing).
    • Network Errors: Ensure database_url is accessible from your server.

Extension Points

  1. Custom Sync Logic: Override syncToFirebase() or syncFromFirebase() in your model:

    public function syncToFirebase()
    {
        $this->setFirebaseData(['custom_key' => 'custom_value']);
        return $this;
    }
    
  2. Firebase Rules: Use this package with custom Firebase rules for security.

  3. Offline Support: Implement local caching for offline-first apps (not built-in; requires custom logic).

  4. Testing: Use FirebaseMock for unit tests:

    $this->partialMock(Mpociot\FirebaseSync\Firebase::class, ['get', 'post', 'put', 'delete']);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle