UNPKG

5.32 kBMarkdownView Raw
1# Connect your app and start prototyping
2
3In this guide, we'll look at how to use `@angular/fire` to connect an Angular application with Firebase Emulator Suite to start prototyping your apps.
4
5There are four supported emulators, all of them available at the Firebase suite workflow:
6
7- [Authentication Emulator](https://firebase.google.com/docs/emulator-suite/connect_auth)
8- [Realtime Database Emulator](https://firebase.google.com/docs/emulator-suite/connect_rtdb)
9- [Cloud Firestore Emulator](https://firebase.google.com/docs/emulator-suite/connect_firestore)
10- [Cloud Functions Emulator](https://firebase.google.com/docs/emulator-suite/connect_functions)
11
12**The Auth Emulator only works with Firebase v8 and above, which is supported by `@angular/fire` 6.1.0 or higher**.
13
14Before configuring these emulators at the Angular App, be sure to install the ones you need by following the [Install, configure and integrate Local Emulator Suite](https://firebase.google.com/docs/emulator-suite/install_and_configure) documentation.
15
16_**TL;DR**_
17
18Initialize firebase to your project (if you haven't) by running:
19
20```shell
21firebase init
22```
23
24Then launch the emulator setup wizard by running:
25
26```shell
27firebase init emulators
28```
29
30Follow the instructions to download whatever emulator you want to use then checkout that the `firebase.json` file got updated with the default ports per emulator, something like this:
31
32```jsonc
33{
34 // Existing firebase configuration ...
35 // Optional emulator configuration. Default
36 // values are used if absent.
37 "emulators": {
38 "firestore": {
39 "port": "8080"
40 },
41 "ui": {
42 "enabled": true, // Default is `true`
43 "port": 4000 // If unspecified, see CLI log for selected port
44 },
45 "auth": {
46 "port": "9099"
47 },
48 "functions": {
49 "port": "5001"
50 },
51 "database": {
52 "port": "9000"
53 },
54 "pubsub": {
55 "port": "8085"
56 }
57 }
58}
59```
60
61## Import the DI Tokens at your AppModule
62
63Configuring your app to connect to local emulators is easily done by using dependency injection tokens provided by the library. However, there are slighty changes between 6.0.0 and 6.1.0 in the way it was done.
64
65### 6.1.0 Method
66
67Each module (database, firestore, auth, function) provides `USE_EMULATOR` token to configure the emulator `host` and `port` by passing a tuple of `[string, number]` values, which are set by default to `localhost` and the asigned port from your `firebase.json` file.
68
69Import these tokens at your `app.module.ts` as follow:
70
71```ts
72import { USE_EMULATOR as USE_AUTH_EMULATOR } from '@angular/fire/compat/auth';
73import { USE_EMULATOR as USE_DATABASE_EMULATOR } from '@angular/fire/compat/database';
74import { USE_EMULATOR as USE_FIRESTORE_EMULATOR } from '@angular/fire/compat/firestore';
75import { USE_EMULATOR as USE_FUNCTIONS_EMULATOR } from '@angular/fire/compat/functions';
76
77@NgModule({
78 // ... Existing configuration
79 providers: [
80 // ... Existing Providers
81 { provide: USE_AUTH_EMULATOR, useValue: environment.useEmulators ? ['localhost', 9099] : undefined },
82 { provide: USE_DATABASE_EMULATOR, useValue: environment.useEmulators ? ['localhost', 9000] : undefined },
83 { provide: USE_FIRESTORE_EMULATOR, useValue: environment.useEmulators ? ['localhost', 8080] : undefined },
84 { provide: USE_FUNCTIONS_EMULATOR, useValue: environment.useEmulators ? ['localhost', 5001] : undefined },
85 ]
86})
87export class AppModule { }
88```
89
90The environment `useEmulators` flag is used to control whenever the app should connect to the emulators, which is usually done in non-production environments.
91
92Also you can opt-in the new way of setting the Cloud Functions [origin](https://firebase.google.com/docs/functions/locations) in Firebase v8 by using the `NEW_ORIGIN_BEHAVIOR` token in conjuction with the already present `ORIGIN` token.
93
94```ts
95import { isDevMode, NgModule } from '@angular/core';
96import { ORIGIN as FUNCTIONS_ORIGIN, NEW_ORIGIN_BEHAVIOR } from '@angular/fire/compat/functions';
97
98@NgModule({
99 // ... Existing configuration
100 providers: [
101 // ... Existing Providers
102 { provide: NEW_ORIGIN_BEHAVIOR, useValue: true },
103 { provide: FUNCTIONS_ORIGIN, useFactory: () => isDevMode() ? undefined : location.origin },
104 ]
105})
106export class AppModule { }
107```
108
109### 6.0.0 Method
110
111With the exception of the Auth Emulator, the old way of setting the `host` and `port` for each emulator was done using a different set of tokens by passing the entire url path as string.
112
113```ts
114import { URL as DATABASE_URL } from '@angular/fire/compat/database';
115import { ORIGIN as FUNCTIONS_ORIGIN } from '@angular/fire/compat/functions';
116import { SETTINGS as FIRESTORE_SETTINGS } from '@angular/fire/compat/firestore';
117
118@NgModule({
119 // ... Existing configuration
120 providers: [
121 {
122 provide: DATABASE_URL,
123 useValue: environment.useEmulators ? `http://localhost:9000?ns=${environment.firebase.projectId}` : undefined
124 },
125 { provide: FIRESTORE_SETTINGS, useValue: environment.useEmulators ? { host: 'localhost:8080', ssl: false } : {} },
126 { provide: FUNCTIONS_ORIGIN, useFactory: environment.useEmulators ? 'http://localhost:5001' : undefined },
127 ]
128})
129export class AppModule { }
130```
131
132For older versions, please upgrade your app to latest version to get the advantages of these new features :rocket:
\No newline at end of file