I have created a firebase account and created the Firebase message class below to send messages but I'm not sure how to send messages to different users.
lass FireMessage {
constructor() {
this.init()
this.checkAuth()
}
init = () => {
if (!firebase.apps.length && firebaseConfig.apps.length === 0) {
firebase.initializeApp({
apiKey: "AIzaSyCjWwL5rs6n-lTlqAkaEh5eK0yXo8",
authDomain: "unihome-36a8.firebaseapp.com",
databaseURL: "https://u36a8.firebaseio.com",
projectId: "u36a8",
storageBucket: "u36a8.appspot.com",
messagingSenderId: "89818921",
appId: "1:898189283761:web:9e09a6566fcb0629dfed90",
measurementId: "G-VYF6PH0MWC"
});
}
};
checkAuth = () => {
firebase.auth().onAuthStateChanged(user => {
if(!user) {
firebase.auth().signInAnonymously();
}
});
};
send = messages => {
messages.forEach(item => {
const message = {
text: item.text,
timestamp: firebase.database.ServerValue.TIMESTAMP,
user: item.user
};
this.db.push(message)
});
};
parse = message => {
const { user, text, timestamp } = message.val();
const { key: _id } = message;
const createdAt = new Date(timestamp);
return {
_id,
createdAt,
text,
user
};
};
get = callback => {
this.db.on("child_added", snapshot => callback(this.parse(snapshot)));
};
off() {
this.db.off();
}
get db() {
return firebase.database().ref("messages");
}
get uid() {
return (firebase.auth().currentUser || {}).uid;
}
}
Am I missing something here? Is it the uid that will be used to message different users in firebase. I'm very new to this and not sure how to go about this. Thanks.