FirebaseDatabase.emulator constructor

FirebaseDatabase.emulator(
  1. FirebaseApp app, {
  2. String host = 'localhost',
  3. required int port,
  4. String? databaseUrl,
  5. String? mockUserToken,
  6. DatabasePersistence? persistence,
  7. DatabaseLogger? logger,
})

Creates the database of app on the Database emulator at host and port.

host is a host name or an IP address; an IPv6 address is given without brackets (::1); [::1] is accepted too. port is 1 to 65535. Throws an ArgumentError for an empty or invalid host and a port out of range.

The client connects over plain WebSocket (over TLS for a *.cloudworkstations.dev host). The database URL (databaseUrl, else the app's databaseURL) then gives only the namespace: its scheme (http, https, ws or wss), host and port are not used to connect, and the client always sends the namespace as ns. http://10.0.2.2:9000?ns=demo and http://my-mac.local:9000?ns=demo both mean namespace demo.

Without ?ns=, the scheme, host and port choose the namespace by the rule the emulator applies to a Host header that names none: take the URL's host:port, without the port when it is the scheme's default (80 for http and ws, 443 for https and wss), split it on ., and use the first part when it is a valid namespace (lowercase letters, digits, -) and, with four or more parts, not a 32-bit integer; else use fake-server. So http://my-mac.local:9000 gives my-mac, https://db.example.com gives db, http://localhost gives localhost, and http://localhost:9000, http://my-mac:9000 (a one-label name with a port) and any IP address give fake-server. When the argument is null and the app's databaseURL is null or empty, or the argument is empty (which skips the app's databaseURL), the namespace is the project's default database <projectId>-default-rtdb; a production URL of that database or ?ns= with that name reach it too. The emulator applies the rules of firebase.json only to the databases firebase.json names, by default <projectId>-default-rtdb; every other namespace has open rules. Measured (our emulator probe, one rules file): only the default namespace got the rules. Read in firebase-tools' code (getRulesConfig), not run: the array form of "database" gives rules to each named instance.

When the namespace came from the host (no ?ns=) and is not <projectId>-default-rtdb, or the app has no project id, logger receives one warning at the first connection of this instance (not again on a reconnect). It names the namespace and the emulator host and says to give ?ns= when firebase.json does not name that namespace; the client cannot read firebase.json, so it also warns when it does. A logger that throws does not stop the connection. A URL with ?ns=, or a null or empty one as above, logs nothing. User info, a path, a fragment, a query key other than ns and an invalid namespace still throw an ArgumentError whose message does not quote the URL. See emulatorFromEnvironment. mockUserToken is sent to the emulator as it is: build a user's token with mockUserTokenFor. A string that is not such a token (the literal 'owner' included) is an admin credential for the emulator and bypasses the rules. An empty one throws an ArgumentError.

persistence and logger are those of the main constructor.

final FirebaseDatabase db = FirebaseDatabase.emulator(
  app,
  port: 9000,
  databaseUrl: 'http://localhost:9000?ns=demo-example-default-rtdb',
);

Implementation

FirebaseDatabase.emulator(
  FirebaseApp app, {
  String host = 'localhost',
  required int port,
  String? databaseUrl,
  String? mockUserToken,
  DatabasePersistence? persistence,
  DatabaseLogger? logger,
}) : this.test(
       app,
       databaseUrl: databaseUrl,
       emulatorHost: _emulatorAddress(host, port),
       mockUserToken: mockUserToken,
       persistence: persistence,
       logger: logger,
     );