Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Firebase chat in React native : How to get all registered users and users I've chatted with?

I am using firestore for my chat application

I have 2 tabs, first one is for to display all registered users and another second tab to display all users I've chatted with.

i have created 2 collections in firestore

  1. users : to store all registered users
  2. chatrooms : to store all messages of particular user

enter image description here

i have combined 2 users id to identify sender and receiver and stored it in messages.

Code:

1. //get all registered users
const querySanp = await firestore()
  .collection('users')
  .where('uid', '!=', user.uid)
  .get();
const allusers = querySanp.docs.map(docSnap => docSnap.data());
setUsers(allusers);

2. //get all registered users
 const docid = uid > user.uid ? user.uid + '-' + uid : uid + '-' + user.uid;

const messageRef = firestore()
  .collection('chatrooms')
  .doc(docid)
  .collection('messages')
  .orderBy('createdAt', 'desc');

const unSubscribe = messageRef.onSnapshot(querySnap => {
  const allmsg = querySnap.docs.map(docSanp => {
    const data = docSanp.data();
    if (data.createdAt) {
      return {
        ...docSanp.data(),
        createdAt: docSanp.data().createdAt.toDate(),
      };
    } else {
      return {
        ...docSanp.data(),
        createdAt: new Date(),
      };
    }
  });
  setMessages(allmsg);
});

How to get users i have chat with ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create a collection :

Conversations: {
   user1,
   user2
}

And in your messages collection:

messages : {
  conversationId,
  sender,
  receiver
}

So you in your app (I suppose you have a screen chats), you just get all the conversations where you are user1 or user2.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!