0

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.

AJDee
  • 147
  • 1
  • 14
  • Hi, I've already implemented code for 1-1 chat using firebase. If you need code then I share it with you. It depends on how you want to implement the chat feature. What that chat includes ( only text message or with file share ) because to add an extra feature that can affect the current documentation structure implemented for a chat. Chat basically runs on a thread system. When One user sends the first message it creates a separate node for that 2 users. where all messages are inserted under that node. – Vinit Bhavsar Nov 21 '21 at 06:44
  • If you want it like WhatsApp like one user delete the entire chat but for the 2nd user it is still there then it becomes more complex to manage. – Vinit Bhavsar Nov 21 '21 at 06:44

0 Answers0