javascript – Why does only writing data to Firebase Database cost me 12kB/minute in downloads?
I am using native Firebase Javascript SDK's
on IoT device running Node-red environment.
- My code consist only
WRITE
andDELETE
operations on the Firebase
RealtimeDatabase data. - When the IoT device turns on the connection is also established.
- But other then that I never
READ
any data from Firebase Realtime
database.
Even though I don’t READ
any data, when I looked this morning into the Firebase console I saw a graph that showed me constant 12kB/minute
downloads and 1
peak connection. I though some bot connected to my database. So I turned off my IoT device and I saw in the graph that also the connection went from 1
to 0
(so I am certain only my IoT device was connected to the database).
Also the downloads went from 12kB/minute
to 0kB/minute
:
- I will add my code below – but I am confused what data did I
downloaded if I didn’t use anyon
,onChildAdded
,onValue
etc.
methods. - I am pretty new to Firebase Database so if I am doing some rookie
mistake just point me to the right direction.
Why does my IoT device download any data when I am only using READ methods?
Code which runs when the IoT device turn’s on:
//Load data from Global contexta
const app = global.get('app');
const database = global.get('database');
const firestore = global.get('firestore');
const auth = global.get('auth');
const firebaseConfig = {
//my credentials
};
//Set up Firebase
const fb_app = app.initializeApp(firebaseConfig);
const fb_db = database.getDatabase(); //kdybychom měli vice projektu tak app dame do parametru
const fb_ft = firestore.getFirestore();
const fb_auth = auth.getAuth();
//Save the database reference to Global context
global.set('fb_app', fb_app);
global.set('fb_db', fb_db);
global.set('fb_ft', fb_ft);
global.set('fb_auth', fb_auth);
Code that writes data every 1 second:
const fb = global.get('database');
const fb_db = global.get('fb_db');
var timestamp = Math.round(msg.payload / 1000);
var UID = 'uid1';
var a1 = 0 + Math.floor(Math.random() * 100);
var p1 = 50 + Math.floor(Math.random() * 20);
var t1 = 20 + Math.floor(Math.random() * 20);
//Send data to Firebase
const ref = fb.ref(
fb_db,
'device_realtime/' + UID + "https://stackoverflow.com/" + timestamp.toString()
);
fb.set(ref, {
a1: a1,
p1: p1,
t1: t1
});
Code that deletes data older then 60 seconds:
const fb = global.get('database');
const fb_db = global.get('fb_db');
var timestamp1 = Math.round(msg.payload / 1000) - 60;
var timestamp2 = Math.round(msg.payload / 1000) - 61;
var UID = 'uid1';
//Delete old data from firebase
var reference1 = fb.ref(
fb_db,
'device_realtime/' + UID + "https://stackoverflow.com/" + timestamp1.toString()
);
var reference2 = fb.ref(
fb_db,
'device_realtime/' + UID + "https://stackoverflow.com/" + timestamp2.toString()
);
fb.remove(reference1)
fb.remove(reference2)
Code that writes data every 1 minute:
const fb = global.get('database');
const fb_db = global.get('fb_db');
var timestamp = Math.round(msg.payload / 1000);
var UID = 'uid1';
var a1 = 0 + Math.floor(Math.random() * 100);
var p1 = 50 + Math.floor(Math.random() * 20);
var t1 = 20 + Math.floor(Math.random() * 20);
//Send data to Firebase
const ref = fb.ref(
fb_db,
'device_trends/' + UID + "https://stackoverflow.com/" + timestamp.toString()
);
fb.set(ref, {
a1: a1,
p1: p1,
t1: t1
});
Screenshot of the entire flow:
Read more here: Source link