capacitor-firebase icon indicating copy to clipboard operation
capacitor-firebase copied to clipboard

feat: Emulator support for Firestore

Open sklink opened this issue 1 year ago • 0 comments

Plugin(s)

  • [ ] Analytics
  • [ ] App
  • [ ] App Check
  • [ ] Authentication
  • [ ] Crashlytics
  • [X] Cloud Firestore
  • [ ] Cloud Messaging
  • [ ] Cloud Storage
  • [ ] Performance
  • [ ] Remote Config

Current problem

Unable to connect to locally running Firestore emulator

Preferred solution

Would like to use the connectFirestoreEmulator method from firebase/firestore to connect to an emulator either at localhost or through a reverse proxy. Note that reverse proxy requires the initializeFirestore(app, { host }); method because connectFirestoreEmulator requires a port.

Alternative options

I did try to use the Firebase SDK methods to connect to the emulator but I can see the network requests still routing through the live Firestore url.

Additional context

I started writing a PR for this but an issue, as mentioned above, is that the connectFirestoreEmulator does not allow for host-only, making reverse proxy unusable. In that case, using initializeFirestore(app, { host }); instead works.

Here's the method for web.ts in capacitor-firebase/firestore, but I'm unsure whether initializeFirestore here is going to cause issues:

  export interface UseEmulatorOptions {
    host: string;
    port?: number;
  }
 
  // ... In the FirebaseFirestoreWeb definition

  public async useEmulator(options: UseEmulatorOptions): Promise<void> {
    const firestore = getFirestore();
    
    if (options.port) {
      connectFirestoreEmulator(firestore, options.host, options.port);
    } else {
      initializeFirestore(firestore.app, {
        host: options.host
      });
    }
  }

I have not looked at the native code to see what might need to be changed there.

Before submitting

  • [X] I have read and followed the feature request guidelines.
  • [X] I have attached links to possibly related issues and discussions.

sklink avatar Mar 29 '24 16:03 sklink