FlutterFire - Working with Functions emulator and actual android device - firebase

I am working in a flutter project and using my personal device for debugging. added, I need to use functions emulator for firebase, but i am keep getting functions unavailable error in flutter, when i try to call my function via emulator.
What I have...
firebase.json
"emulators": {
"auth": {
"host" : "0.0.0.0",
"port": 9099
},
"functions": {
"host" : "0.0.0.0",
"port": 5001
},}
in main.dart
FirebaseFunctions.instance.useFunctionsEmulator("172.20.10.7", 5001); // my system ip in local network
in pubspec.yaml
firebase_auth: ^3.3.6
cloud_firestore: ^3.1.7
cloud_functions: ^3.2.7
firebase_database: ^9.0.6
where 172.20.10.7 => this is my system ip in local network..
I am using Ubuntu 20.04.3 LTS, I don't know what should I do to overcome this.

Related

Firebase functions app emulator return "Function europe-west1-app does not exist, valid functions are: "

My firebase functions express app is not allowing me to call it when emulating.
The emulator seems to be running fine:
The logs in a better format:
i emulators: Starting emulators: functions
⚠ hub: Error when trying to check port 4400 on ::1: Error: listen EADDRNOTAVAIL: address not available ::1:4400
⚠ hub: Port 4400 is available on 127.0.0.1 but not ::1. This may cause issues with some clients.
⚠ hub: If you encounter connectivity issues, consider switching to a different port or explicitly specifying "host": "<ip address>" instead of hostname in firebase.json
⚠ logging: Error when trying to check port 4500 on ::1: Error: listen EADDRNOTAVAIL: address not available ::1:4500
⚠ logging: Port 4500 is available on 127.0.0.1 but not ::1. This may cause issues with some clients.
⚠ logging: If you encounter connectivity issues, consider switching to a different port or explicitly specifying "host": "<ip address>" instead of hostname in firebase.json
⚠ eventarc: Error when trying to check port 9299 on ::1: Error: listen EADDRNOTAVAIL: address not available ::1:9299
⚠ eventarc: Port 9299 is available on 127.0.0.1 but not ::1. This may cause issues with some clients.
⚠ eventarc: If you encounter connectivity issues, consider switching to a different port or explicitly specifying "host": "<ip address>" instead of hostname in firebase.json
⚠ functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, hosting, pubsub, storage
✔ functions: Using node#16 from host.
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "/app/functions" for Cloud Functions...
⚠ functions: package.json indicates an outdated version of firebase-functions. Please upgrade using npm install --save firebase-functions#latest in your functions directory.
⚠ functions: Please note that there will be breaking changes when you upgrade.
✔ functions: Loaded functions definitions from source: .
The emulator UI:
The code:
import * as functions from "firebase-functions"
import * as admin from "firebase-admin"
import express, { Express } from "express"
const bodyParser = require("body-parser")
const cors = require("cors")
const routes = require("./routes")
if (admin.apps.length === 0) {
admin.initializeApp()
}
const app: Express = express()
app.use(cors({ origin: true }))
app.use("/api/v2", routes)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
exports.app = functions.region("europe-west1").https.onRequest(app)
Calling http://0.0.0.0:8087/MY-PROJECT-NAME/europe-west1/app/api/v2 and any subsquent route produces the error message:
Function europe-west1-app does not exist, valid functions are: and then no valid functions.
My emulator is in a docker container with port mappings 8087:8080 (hence the above URL).
I have tried a number of combinations for the endpoint to no avail. I am clearly missing something.
What am I doing wrong?
Routes look like:
router.get("/", centre.hello) // This one return a hello string that I've been using to troubleshoot. no luck
router.post("/centre/add", centre.addCentre)
Edited to add additional logs as requested
I'm an idiot.
Although I had exported "app" as above, I had not provided an index.ts which I believe firebase needed.
I realised this when I deployed the functions live and the error message was a lot more helpful.
Emulators now work as expected

Check if Firebase Emulator is connected from Node scripts

I am writing unit tests for my Firebase Functions and I want to automatically connect the functions, auth, storage, etc. emulators from my script without having to specify if I am testing in local environment or development environment.
Is there any way I can write a script to see if the Firebase Emulator is running on my local machine from an external node script?
For example, is there a way I can see processes running on specific local ports from a node script?
I tried using
import { exec } from "child_process";
const checkEmulator = exec("lsof -i:5000");
(I am using MacOS)
Then using the output to determine if the Firebase Functions Emulator is running on port 5000, but the output of the exec function does not make any sense to me.
Is there a more efficient way to check if the emulator is running on your local machine?
Thanks for any help!
You can use an HTTP Get request with something like curl:
curl localhost:4400/emulators
This will print out a JSON object listing all running emulators. Example:
{
"hub":{
"name": "hub",
"host": "localhost",
"port": 4400
},
"functions": {
"name": "functions",
"host": "localhost",
"port": 5001
}
"firestore": {
"name": "firestore",
"host": "localhost",
"port": 8080
}
}
Taken from https://firebase.google.com/docs/emulator-suite/install_and_configure
Alternatively, psaux can list active processes. Each emulator is a different process and is run from .../.cache/firebase/emulators.
https://www.npmjs.com/package/psaux

Unable to access firebase emulator UI

I have the following firebase.json
{
"emulators": {
"firestore": {
"port": 8080
},
"ui": {
"enabled": true,
"host": "localhost",
"port": 4000
},
"database": {
"port": 9000
}
}
}
But I can't access localhost:4000.
My firebase-tools version is 8.4.3.
Running firebase emulators:start shows the following:
i emulators: Starting emulators: firestore, database
⚠ firestore: Did not find a Cloud Firestore rules file specified in a firebase.json config file.
⚠ firestore: The emulator will default to allowing all reads and writes. Learn more about this option: https://firebase.google.com/docs/emulator-suite/install_and_configure#security_rules_configuration.
i firestore: Firestore Emulator logging to firestore-debug.log
⚠ database: Did not find a Realtime Database rules file specified in a firebase.json config file. The emulator will default to allowing all reads and writes. Learn more about this option: https://firebase.google.com/docs/emulator-suite/install_and_configure#security_rules_configuration.
i database: Database Emulator logging to database-debug.log
┌──────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! It is now safe to connect your apps. │
└──────────────────────────────────────────────────────────────┘
┌───────────┬────────────────┐
│ Emulator │ Host:Port │
├───────────┼────────────────┤
│ Firestore │ localhost:8080 │
├───────────┼────────────────┤
│ Database │ localhost:9000 │
└───────────┴────────────────┘
Other reserved ports:
Thank you in appreciation.
I made a mistake of not choosing a default project.
With a chosen default project, the emulator ui came up.
In case anyone is using docker for the emulator and trying to access it from the host using port mappings, make sure to set the host property of the emulator UI config to 0.0.0.0. This goes for all other services you want to access from the host as well.
{
"emulators": {
"ui": {
"host": "0.0.0.0",
"port": 4000
},
// other services
}
Make sure in firebase.json , ui is set to true for emulators like below
"emulators": {
"pubsub": {
"port": 8085
},
"ui": {
"enabled": true
}, }
Then once you run emulator with "firebase emulator:start"
You will find
If you visit mentioned url in browser, you will find

Unable to connect to firebase emulator suite with exec

Started firebase project emulators (which uses cloud functions and firestore) with below command
firebase emulators:start
It runs successfully and gives me a path to connect to the functions and shows a local host url for firestore too.
Then, to execute my jest tests, ran the below command
firebase emulators:exec --only firestore jest
As per the documentation, to connect to local firstore, we need to use exec. But its throwing below error.
i emulators: Starting emulators: firestore
⚠ emulators: emulator hub unable to start on port 4400, starting on 4401
✔ hub: emulator hub started at http://localhost:4401
i Shutting down emulators.
i Stoppping emulator hub
⚠ Port 8080 is not open on localhost, could not start firestore emulator.
i To select a different host/port for the emulator, update your "firebase.json":
{
// ...
"emulators": {
"firestore": {
"host": "HOST",
"port": "PORT"
}
}
}
i Shutting down emulators.
Error: Could not start firestore emulator, port taken.
This error is thrown everytime when I run exec command. Can someone point out what could be wrong?
Edit: Logs from firebase emulators:start
firebase emulators:start
i emulators: Starting emulators: functions, firestore, hosting, pubsub
✔ hub: emulator hub started at http://localhost:4400
⚠ Your requested "node" version "8" doesn't match your global version "10"
✔ functions: functions emulator started at http://localhost:5001
i firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080
⚠ firestore: Support for WebChannel on a separate port (8081) is DEPRECATED and will go away soon. Please use port above instead.
i firestore: firestore emulator logging to firestore-debug.log
✔ firestore: firestore emulator started at http://localhost:8080
i firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
i hosting[website]: Serving hosting files from: public
✔ hosting[website]: Local server: http://localhost:5000
i hosting[admin]: Serving hosting files from: public
✔ hosting[admin]: Local server: http://localhost:5005
i hosting[b2b]: Serving hosting files from: public
✔ hosting[b2b]: Local server: http://localhost:5006
i hosting[b2c]: Serving hosting files from: public
✔ hosting[b2c]: Local server: http://localhost:5007
i hosting[sdk]: Serving hosting files from: public
✔ hosting[sdk]: Local server: http://localhost:5008
✔ hosting: hosting emulator started at http://localhost:5000
i pubsub: pubsub emulator logging to pubsub-debug.log
✔ pubsub: pubsub emulator started at http://localhost:8085
Update
With a fresh start also the mentioned error is shown. But killing the port made it work.
Added below in scipts part of package.json
"kill": "npx kill-port 5000 5001 8080 8085 4000 9229"
and run
npm run kill
Here all above listed ports aren't reqired. Just 8080 might work in your case but I have other ports too being used by the emulator.
lsof command is not availalble on windows powershell.
A better cross-platform solution is: npx kill-port 8080
Type this in your terminal, where 8080 is your port number:
lsof -ti tcp:8080 | xargs kill
Reason: Failed quitting the previous firebase emulator.
There is an issue filed for firepit (the standalone CLI installable with curl): #1868
Installing it as Node package may help: npm install -g firebase-tools
If this should already be the case, you probably should file an issue there.
I closed the terminal, then opened it again, launched command again and it works

Mupx deployment with Meteor.js fails when "Installing Docker"

I have Ubuntu 14.04, developing on it and want to have a test server on the same computer. It is ran in Virtual Box.
So I followed all the steps on Github for Mupx setup and watched the video that Meteor.js guide told me to watch it. When I get to command:
mupx setup
it shows me the screen with the error:
nejc#nejc-vb:~/Meteor Projects/CSGO/CSGO-deploy$ mupx setup
Meteor Up: Production Quality Meteor Deployments
------------------------------------------------
Configuration file : mup.json
Settings file : settings.json
“ Checkout Kadira!
It's the best way to monitor performance of your app.
Visit: https://kadira.io/mup ”
Started TaskList: Setup (linux)
[my_public_IP] - Installing Docker
events.js:72
throw er; // Unhandled 'error' event
^
Error: Timed out while waiting for handshake
at null._onTimeout (/usr/local/lib/node_modules/mupx/node_modules/nodemiral/node_modules/ssh2/lib/client.js:138:17)
at Timer.listOnTimeout [as ontimeout] (timers.js:121:15)
My mup.json file looks like this:
{
// Server authentication info
"servers": [
{
"host": "my_public_IP",
"username": "nejc",
"password": "123456",
// or pem file (ssh based authentication)
// WARNING: Keys protected by a passphrase are not supported
//"pem": "~/.ssh/id_rsa"
// Also, for non-standard ssh port use this
//"sshOptions": { "port" : 49154 },
// server specific environment variables
"env": {}
}
],
// Install MongoDB on the server. Does not destroy the local MongoDB on future setups
"setupMongo": true,
// Application name (no spaces).
"appName": "CSGO",
// Location of app (local directory). This can reference '~' as the users home directory.
// i.e., "app": "~/Meteor Projects/CSGO",
// This is the same as the line below.
"app": "/home/nejc/Meteor Projects/CSGO",
// Configure environment
// ROOT_URL must be set to your correct domain (https or http)
"env": {
"PORT": 80,
"ROOT_URL": "http://my_public_IP"
},
// Meteor Up checks if the app comes online just after the deployment.
// Before mup checks that, it will wait for the number of seconds configured below.
"deployCheckWaitTime": 30,
// show a progress bar while uploading.
// Make it false when you deploy using a CI box.
"enableUploadProgressBar": true
}

Resources