React-Native-Firebase : RTDB Emulator - javascript

Does anybody knows how to use realtime database emulators?
I can use function & firestore emulators like below.
import functions from '#react-native-firebase/functions';
import firestore from '#react-native-firebase/firestore';
functions().useFunctionsEmulator('http://localhost:5001');
firestore().settings({ host: 'localhost:8080' });
But was not able to find something similar for realtime database.
Any link/video is appreciated.
Thanks.

Basically the solution posted by Frank van Puffelen was correct. However, this got me stuck for a while so I'm going to share my experience to save time for others.
import { firebase } from "#react-native-firebase/database";
const database = firebase
.app()
.database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");
const ref = database.ref(`yourPath`)
...
Here comes the huge gotcha:
If you're using Android Emulator you have to use
const database = firebase
.app()
.database("http://10.0.2.2:9000?ns=YOUR_DATABASE_NAMESPACE");
If you're using a real android device you have to set up reverse proxy first:
adb reverse tcp:9000 tcp:9000
then set up the database normally
const database = firebase
.app()
.database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");
I didn't test for iOS, but I believe localhost should work.

I have this in my root index.js file for my react native project:
// Use a local emulator in development
if (__DEV__) {
// If you are running on a physical device, replace http://localhost with the local ip of your PC. (http://192.168.x.x)
auth().useEmulator('http://localhost:9099');
functions().useFunctionsEmulator('http://localhost:5001');
database().useEmulator('localhost', 9000);
const db = firestore();
db.settings({ host: 'localhost:8080', ssl: false });
}
Nothing else in my app needed to be modified. The database().useEmulator line does the trick.
I am assuming you have already initialized all the emulators first using firebase init emulators and have them running with firebase emulators:start.
Make sure you're using the ${projectId}-default-rtdb database in the emulator.

According to the documentation on Connect your app to the Realtime Database Emulator, that is done with:
if (location.hostname === "localhost") {
var firebaseConfig = {
// Point to the RTDB emulator running on localhost.
// In almost all cases the ns (namespace) is your project ID.
databaseURL: "http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE"
}
var myApp = firebase.initializeApp(firebaseConfig);
var db = myApp.database();
}
I'm not certain if that is supported in react-native-firebase yet, since it isn't mentioned in their documentation at all. The most logical things to try would be their Using a secondary database:
const database = firebase.app().database('http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE');

Related

How to deploy Next JS App with Mongoose(MongoDB) connection on Vercel?

I am facing issue while deploying my next js app on vercel with mongodb connection. I have added env variable also on vercel site where we deploy next js app. Is there something going wrong in the below file ?
next.config.js
module.exports = {
env: {
MONGODB_URI: "mongodb://localhost/tasksdb",
},
};
I have add env variable as well into my next js project
.env.local
MONGODB_URI = "mongodb://localhost/tasksdb"
I establish this mongodb connection from this doc https://mongoosejs.com/. It's provide us to connect with mongodb straightaway .
And this my mongodb connection establish code
import { connect, connection } from "mongoose";
const conn = {
isConnected: false,
};
export async function dbConnect() {
if (conn.isConected) return;
const db = await connect(process.env.MONGODB_URI);
conn.isConnected = db.connections[0].readyState;
// console.log(conn.isConnected);
// console.log(db.connection.db.databaseName)
}
connection.on("connected", () => {
console.log("Mongodb connected to db");
});
connection.on("error", (err) => {
console.error("Mongodb connected to", err.message);
});
Is there something wrong by creating the database this way because in local everything working fine ?
I don't think vercel's servers can comunicate with your localhost. You can try hosting your database in the mongodb cloud
The code seems to be fine, but you can not connect to Vercel using localhost. You can use Serverless Atlas version of MongoDB which is free you can host your database there and then connect using the link they will provide.
See : https://www.mongodb.com/pricing

Create multiple Firebase Instances for the same project in Node.js

I have a Node.js server, inside which I want to have two firebase instances.
One instance should use the JavaScript SDK and will be used to provide authentication - login/register. The other instance should use the Admin SDK and will be used to read/write from the Realtime Database. I want to use this approach, so that I don't have to authenticate the user before each request to the Realtime DB.
I've read how we're supposed to initialize Firebase instances for multiple projects, but I'm not sure if my issue isn't coming from the fact that both instances are for the same project.
My issue is that I can use the JS SDK without any issue and I can login/register the user, but for some reason I can't get the Admin SDK to work.
Here's how I'm instantiating the apps:
const admin = require("firebase-admin");
const { applicationDefault } = require('firebase-admin/app');
admin.initializeApp({
credential: applicationDefault(),
databaseURL: 'my-database-url'
}, 'adminApp');
const firebase = require("firebase/app");
firebase.initializeApp(my-config);
Now I can use the JS SDK without an issue, but not the Admin SDK. I've created a test endpoint to just get data from my Realtime DB:
app.get("/api/test", (req, res) => {
const uid = 'my-user-UID';
admin.database().ref(`users/${uid}`)
.once('value', (snapshot) => {
if(snapshot) {
console.log('data');
} else {
console.log('no data');
}
});
});
Now here as an approach to getting the data from the Realtime DB, I tried all possible described approaches. Using get with child and all sorts of possible combinations. Here's an example of another approach I used:
get(child(ref(admin.database()), `users/${uid}`)).then((snapshot) => {
if (snapshot.exists()) {
// retrieved data
} else {
// No data
}
}).catch((error) => {
console.error(error);
});
For the first approach I wasn't getting any response at all, like the once wasn't executing. For the second one I think I was getting - typeerror: pathstring.replace is not a function firebase. At some point I was getting a no firebase app '[default]' has been created . These errors don't worry me as much, but since I saw the last error I moved my focus to the initialization of the apps, but still to no avail.
I just need a direction of where my issue might be coming from.
Update:
The solution is to not pass a second argument (app name) to any of the Firebase initializations. Looks like it's not needed in case you're referencing the same project.

firebase how do I test if emulator is running?

I am connecting to my storage emulator with the following code:
const firebaseApp = initializeApp(appConfig.firebase)
storage = getStorage(firebaseApp)
connectStorageEmulator(storage, 'localhost', 9199)
This works fine when the emulator is running. It also works fine when the emulator is NOT running! if its not running I want to do something else like this:
if (emulatorIsConnectedAndEmulatorIsRunning) {
// do something
} else {
// do something else
}
How can I detect whether the emulator is running or not?
TIA
This is AN answer, not necessarily the best answer and I would appreciate other answers.
Basically, I am not just emulating storage but also cloud functions.
I've made a cloud function like this:
export const ping = functions.https.onRequest((req, res) => {
res.json({ ping: 'pong' })
})
So in my code I just ping this endpoint. For example:
axios.get(`http://localhost:5001/${storageBucket}/us-central1/ping`)
If it responds 200 then the function emulator is running (and given my setup this also means the storage emulator is running).

How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions?

I want to reset a specific value in my Firebase Realtime Database every day at 12:00 AM. To do this, I'm using the Firebase Admin SDK to change the data in Firebase Realtime Database and Cloud Functions to trigger the change at 12:00 AM every day.
This is an example structure of my Firebase Realtime Database:
{
"users": {
"fa54487d9cbb4214b00db80e2118e4e6": {
"daily": 10
}
}
}
This is the code in my index.js:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
var functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore.
var admin = require('firebase-admin');
// Fetch the service account key JSON file contents
var serviceAccount = require("serviceAccountKey.json");
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databaseName.firebaseio.com"
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("users");
// Reset today GHG emissions at 12:00 AM everyday
exports.dailyReset = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
usersRef.child("{userId}").set({
daily: 0
});
});
Deploy Error:
! functions[dailyReset(us-central1)]: Deployment error.
Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.
Firebase Console Functions Logs:
Error: function terminated. Recommended action: inspect logs for termination reason.
Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging Function cannot be initialized.
{"#type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Function failed on loading user code. This is likely due to a bug in the user code.
Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs.
Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging.
The script won't deploy when I use firebase deploy as my function is giving me an error. Can someone tell me how to fix my code?
This won't work:
exports.dailyReset = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
usersRef.child("{userId}").set({
daily: 0
});
});
There is nothing here that interprets the {userId} in that path, so the database updates the literal path "/users/{userId}", which is not what you want.
If you know what user ID you want to update, you should use that value in the path:
exports.dailyReset = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
let usersRef = admin.database().ref("users");
usersRef.child("theActualUserIdYouWantToUpdate").set({
daily: 0
});
});
If you don't know what user ID to update, you'll need to query the database to determine that.
If you want to loop over all users, you can do:
exports.dailyReset = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
return usersRef.once("value").then((snapshot) => {
let updates = {};
snapshot.forEach((userSnapshot) => {
updates[userSnapshot.key+"/daily"] = 0
});
return usersRef.update(updates);
});
});
If you are new to JavaScript or interacting with the Realtime Database in it, Cloud Functions for Firebase is not the best way to learn it. I recommend first reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer. They cover many basic JavaScript, Web and Firebase interactions. You could also use the Admin SDK in a local Node.js process, which can be debugged with a local debugger. After those you'll be much better equipped to write code for Cloud Functions too.

How to put a trigger to another Firebase Database from Firebase Functions?

I'm trying to trigger my database function when something is wrote to a database from an associate.
I know that I need a Service Account created in the other database and the .json file that firebase gives to do the connection properly. Right now I'm giving all the permissions for getting sure than my errors don't come from this.
With what I found on the documentation and with other information on internet, this is how I login to the other database:
var adminAbi = require("firebase-admin");
var functionsAbi = require('firebase-functions');
const serviceAccount = require(`./serviceacountfile.json`);
adminAbi.initializeApp({
credential: adminAbi.credential.cert(serviceAccount),
databaseURL: 'https://DATABASEURL.firebaseio.com/',
},'test' );
And this is my trigger:
exports.copyDatabasess = functionsAbi.database.instance('test').ref('/messages/{user_id}/{now}').onWrite(event =>{
if (!event.data.exists()) {
return;
}
console.log('copydatabase', event.params.body);
// Grab the current value of what was written to the Realtime Database.
const original = event.data.val();
});
With this code I'm getting right now this error when I deploy:
! functions[copyDatabasess]: Deployment error. Failed to configure
Firebase Realtime Database trigger: unknown error, HTTP code 401
I really can't find useful informations about this error and how to solve it. If someone knows something about this would be much appreciated.
Thanks in advice.
You can't put a trigger on a database that's not in the same project as your functions. instance() only works with database shards in the same project.

Categories

Resources