Skip to content

Commit 7193918

Browse files
authored
docs(): Local Emulator Suite (#2697)
* docs(emulators): add emulators setup info * docs(emulators): use jsonc markdown syntax
1 parent 1773837 commit 7193918

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ Firebase offers two cloud-based, client-accessible database solutions that suppo
117117
- [Getting started with Firebase Authentication](docs/auth/getting-started.md)
118118
- [Route users with AngularFire guards](docs/auth/router-guards.md)
119119

120+
### Local Emulator Suite
121+
122+
- [Getting started with Firebase Emulator Suite](docs/emulators/emulators.md)
123+
120124
### Upload files
121125

122126
- [Getting started with Cloud Storage](docs/storage/storage.md)

Diff for: docs/emulators/emulators.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Connect your app and start prototyping
2+
3+
In 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+
5+
There 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+
14+
Before 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+
18+
Initialize firebase to your project (if you haven't) by running:
19+
20+
```shell
21+
firebase init
22+
```
23+
24+
Then launch the emulator setup wizard by running:
25+
26+
```shell
27+
firebase init emulators
28+
```
29+
30+
Follow 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+
63+
Configuring 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+
67+
Each 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+
69+
Import these tokens at your `app.module.ts` as follow:
70+
71+
```ts
72+
import { USE_EMULATOR as USE_AUTH_EMULATOR } from '@angular/fire/auth';
73+
import { USE_EMULATOR as USE_DATABASE_EMULATOR } from '@angular/fire/database';
74+
import { USE_EMULATOR as USE_FIRESTORE_EMULATOR } from '@angular/fire/firestore';
75+
import { USE_EMULATOR as USE_FUNCTIONS_EMULATOR } from '@angular/fire/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+
})
87+
export class AppModule { }
88+
```
89+
90+
The environment `useEmulators` flag is used to control whenever the app should connect to the emulators, which is usually done in non-production environments.
91+
92+
Also 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
95+
import { isDevMode, NgModule } from '@angular/core';
96+
import { ORIGIN as FUNCTIONS_ORIGIN, NEW_ORIGIN_BEHAVIOR } from '@angular/fire/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+
})
106+
export class AppModule { }
107+
```
108+
109+
### 6.0.0 Method
110+
111+
With 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
114+
import { URL as DATABASE_URL } from '@angular/fire/database';
115+
import { ORIGIN as FUNCTIONS_ORIGIN } from '@angular/fire/functions';
116+
import { SETTINGS as FIRESTORE_SETTINGS } from '@angular/fire/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+
})
129+
export class AppModule { }
130+
```
131+
132+
For older versions, please upgrade your app to latest version to get the advantages of these new features :rocket:

0 commit comments

Comments
 (0)