I have this block of code in my /functions/src/index.ts:
export const onAddReviewerSendEmailNotification = functions
.runWith({
timeoutSeconds: 15,
})
.firestore.document("records/{email}/managedByOthers/{reviewerEmail}")
.onWrite( (snap, context) => {
functions.logger.info("something changed!");
functions.logger.debug(`Running add reviewer trigger for userId: ${context.params.email}, reviewerId: ${context.params.reviewerEmail}`);
});
I compile it using npm run build in the functions directory. Then I run the emulator using this command:
firebase emulators:start --only firestore,storage,functions --import test-data
This is what I get:
I can see emulator UI is running. I go to firestore and add a record in this path: records/{email}/managedByOthers/{reviewerEmail}. Then I go to emulator's Logs section. Nothing has been logged. Am I doing something wrong?
I finally found the solution after wasting 4 hours on the issue. I replaced
"functions": {
"port": 5001
},
by
"functions": {
"host": "0.0.0.0",
"port": 5001
},
in the firebase.json file. I am able to see the logs now!
Related
I am trying to build a scheduled function on Firebase using the emulator. I've set things up as shown and have verified that PubSub is running on my emulator. However, nothing happens.
I have the following function:
exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
console.log('This will be run every 5 minutes!');
return null;
});
I can verify the function works using firebase functions:shell:
firebase functions:shell
i functions: Loaded functions: createUserDocument, newSlackUserNotify, scheduledFunction
⚠ functions: The following emulators are not running, calls to these services will affect production: firestore, database, pubsub, storage, eventarc
firebase > scheduledFunction()
'Successfully invoked function.'
firebase > > This will be run every 5 minutes!
My firebase.json:
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"emulators": {
"auth": {
"port": 9099,
"host": "0.0.0.0"
},
"firestore": {
"port": 8080,
"host": "0.0.0.0"
},
"functions": {
"port": 5001,
"host": "0.0.0.0"
},
"pubsub": {
"port": 8085,
"host": "0.0.0.0"
},
"ui": {
"enabled": true
},
"singleProjectMode": true
},
"functions": [
{
"source": "apps/functions",
"codebase": "default",
"ignore": ["node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log"],
}
]
}
I can see here that someone suggests using a trigger, but shouldn't scheduled functions "just work"?
I have also verified that the scheduled function works when deployed, so this seems to be an emulator-specific thing.
The only possible thing is that SLF4J seems to have not loaded, per pubsub-debug.log:
This is the Google Pub/Sub fake.
Implementation may be incomplete or differ from the real system.
Jan 13, 2023 2:09:25 PM com.google.cloud.pubsub.testing.v1.Main main
INFO: IAM integration is disabled. IAM policy methods and ACL checks are not supported
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jan 13, 2023 2:09:26 PM com.google.cloud.pubsub.testing.v1.Main main
INFO: Server started, listening on 8085
Jan 13, 2023 2:09:31 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.
As Chris mentioned in above comment.Emulator doesn't support scheduled functions yet. There is a feature request raised for Support for scheduled functions in emulator in github. You can also add your concern over there. You need to trigger them manually using the firebase functions:shell command .You can run the scheduled functions on a interval:
firebase > setInterval(() => yourScheduledFunc(), 60000)
//This will run your functions every 60 seconds.
You can also check this stackoverflow thread1 & thread2
My app fails to connect to the Firestore emulator over the local network. I can access Firestore fine on the same PC the emulator is running on (localhost essentially), but when I test the app over the local network on my iPhone (http://192.168.1.23) anything to do with Firestore fails.
My code in firebase.json is:
"emulators": {
"firestore": {
"port": 8080
}
}
And in my firebaseInit.js file it's connectFirestoreEmulator(db, 'localhost', 8080)
In firebase.json I've tried adding a host property as follows:
"emulators": {
"firestore": {
"port": 8080,
"host": "192.168.1.23"
}
}
...and I've also tried "host":"http://192.168.1.23" and "host":"0.0.0.0"
In my firebaseInit.js file I've also tried:
connectFirestoreEmulator(db, '192.168.1.23', 8080)
and
connectFirestoreEmulator(db, 'http://192.168.1.23', 8080)
Any help would be greatly appreciated. Thanks
I managed to solve it; I must've missed this particular configuration when I was trying different hosts and ports in various places. But for me it now works with firebase.json file as:
"emulators": {
"firestore": {
"host": "192.168.1.23",
"port": 8080
},
...and firebaseInit.js file with the line:
connectFirestoreEmulator(db, '192.168.1.23', 8080)
Not sure how I missed this specific combination but I'll leave this here for anyone else who has this problem. Thanks all.
whenever I run a local server of my HTML page I get this error in the console (cloud functions are NOT being hosted locally):
GET https://www.gstatic.com/firebase/9.16.0/firebase-firestore.js net::ERR_ABORTED 404
GET https://www.gstatic.com/firebase/9.16.0/firebase-functions.js net::ERR_ABORTED 404
I am aware that this is a topic that has been asked about before, but none of the solutions others came to has helped me in any way. The other modules load fine. Here is my reference to all the modules in my HTML:
<script src="https://www.gstatic.com/firebasejs/8.8.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.8.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.8.0/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebase/9.16.0/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebase/9.16.0/firebase-functions.js"></script>
Here is my firebase config at the beggining of my JS:
var firebaseConfig = {
apiKey: "AIzaSyB_gTZfsFfyrn_xsnNmtWuUMQkoyuHztts",
authDomain: "form-ce7e9.firebaseapp.com",
projectId: "form-ce7e9",
storageBucket: "form-ce7e9.appspot.com",
messagingSenderId: "479728355456",
appId: "1:479728355456:web:e7ce8a6a21e33cd176fe65",
measurementId: "G-GHTPHK5QXS"
Here is my index.js which I have deployed successfully:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
// add trigger (new user signup)
exports.newUserSignup = functions.auth.user().onCreate((user) => {
// for background triggers you must return a value/promise
return admin.firestore().collection("users").doc(user.id).set({
email: user.email,
cybos: [],
});
});
And finally here is my firebase.JSON:
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
Any help we be greatly appreciated, I'm about to be tearing my hair out
UPDATE:
This message appeared another time I ran the HTML on a local server:
Failed to load resource: the server responded with a status of 404 ()
It appears twice, once for firestore and once for functions
If anyone else is running into this issue it was actually a really simple solution, so I feel pretty dumb. I had the incorrect url's for the script reference, check this link for all of the appropriate links. My version number was incorrect is what it boiled down to.
I am working on a swiftui app and just started using the Emulator suite of firebase.
Everything is fine when i preview the app and write to a firestore collection or authenticate a user. The problem is that whenever i want to test it on a simulator or a actual physical device it does nothing. And i cannot find anything online regarding using emulator with a physical device in your local lan.
What could be stopping my physical device to make a connection with my Macbook where the emulators are running?
See my firebase.json config below:
NOTE: i have tryed to put "host": "192.168.x.x" or "0.0.0.0" <- my macbook ip adres
But with no result.
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"storage": {
"rules": "storage.rules"
},
"emulators": {
"auth": {
"port": 9099
},
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"storage": {
"port": 9199
},
"ui": {
"enabled": true
}
}
}
code below FirebaseApp.configure()
Where Localhost is "192.168.x.x" my macbook IP
let settings = Firestore.firestore().settings
settings.host = "localhost:8080"
settings.isPersistenceEnabled = false
settings.isSSLEnabled = false
Firestore.firestore().settings = settings
In the info.plist i added App transport Security Settings:
Allows local networking = YES
What i see in debug.log (This might be unrelated)
Jul 04, 2021 10:20:53 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
What could be a problem to stop my phone from connecting to my firebase emulators hosted on my macbook which is in the same local network as my phone.
When i use firebase cloud version everything works fine.
Could it be something in my firewall or on the macbook itself?
Which would explain that both physical and a simulator cannot write /read to firestore.
Hope some one can help me!
Would love to test functions and everything else locally first.
Greetings,
Dylan
I am using the Realtime Database and Functions for my Flutter app. I've set up local emulators for both Database and Functions, so I can test Functions locally without the deployment cost. Now I want to connect my Flutter app to these local emulators, but I am having some trouble finding out how to do it.
The FlutterFire docs describes how to do this for Firestore (https://firebase.flutter.dev/docs/firestore/usage#emulator-usage), but not for the Realtime Database. Surely there must be a way to connect to the emulators, even though I am using the Realtime Database?
I have tried the following:
String host = Platform.isAndroid ? '10.0.2.2:4000' : 'localhost:4000';
FirebaseDatabase database = FirebaseDatabase(
app: Firebase.app(),
databaseURL: host,
);
But that gives me the error:
Unhandled Exception: PlatformException(error, Invalid Firebase Database url specified: 10.0.2.2:4000, null)
I am using the firebase_core and firebase_database packages on pub.
Digging deeper into Firebase Java SDK, I've found that .useEmulator() only sets host and port for creating the databaseURL and in short is just a refactoring wrapper.
Code below should work with FlutterFire Realtime Database package:
String host = Platform.isAndroid ? 'http://10.0.2.2:9000' : 'http://localhost:9000';
FirebaseDatabase database = FirebaseDatabase(
app: Firebase.app(),
databaseURL: host,
);
Also, do check your firebase.json for the correct port to use. Default should be 9000 for database.
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
],
"source": "functions"
},
"storage": {
"rules": "storage.rules"
},
"emulators": {
"auth": {
"port": 9099
},
"functions": {
"port": 5001
},
"firestore": {
"port": 8080
},
"database": {
"port": 9000
},
"ui": {
"enabled": true
}
}
}
According to the Firebase documentation for Android, using the Realtime Database emulator requires a call database.useEmulator("10.0.2.2", 9000). A quick search of the FlutterFire code shows no such calls, so it seems that emulator usage isn't implemented in FlutterFire yet.
I also couldn't find an open issue for it, so you might want to file a feature request.
Following code worked for me. Its almost similar to #Florin Stan answer. Just the namespace of the database was missing.
String host = Platform.isAndroid ? 'http://10.0.2.2:9000' : 'http://localhost:9000';
dbInstance= FirebaseDatabase(
databaseURL: '$host?ns=YOUR_DATABASE_NAMESPACE',
);
Database namespace can be found in firebase realtime database emulator UI.