0

I am using GiftedChat for RN and its working perfectly, but I can't manage proper message ordering by time if different devices have different time.

For example I have two devices that have manual time setup on them. At a specific time (let's say 12:00) the dates on the devices are: iOS - 12:00, Android: 12:03.

  1. I would send a message from iOS to Android device at 12:01 (iOS Time) and it would show on Android at that time too (12:01, even though Android's time is 12:04).

  2. Then, I would send a message back to iOS from Android at 12:04 (Android time) and it would show up in the right position on iOS. However, the time would be 12:04 on the message (iOS device time at that moment would be 12:01).

  3. Last, if I send a message again from iOS at 12:02 (Android - 12:05) it would appear on Android above the last message (wrong order).

What should I do? I tried to fetch timestamps from the firebase server, but that would mean that they might not match with the actual time on the devices.

Something like firebase.firestore.Timestamp.now().seconds would not work either because it shows client time only.

Any ideas?

Basics of my implementation:

onSend():

db.collection(`${companyId.toString()}`)
    .doc("chats")
    .collection(`chats-${chatRoom.toString()}`)
    .add({
        _id,
        createdAt,
        text,
        user,
    });

onReceive():

snapshot.docs.map((doc) => ({
    _id: doc.data()._id,
    createdAt: doc.data().createdAt.toDate(),
    formattedDate: doc.data().formattedDate,
    text: doc.data().text,
    user: doc.data().user,
}))
denistepp
  • 489
  • 7
  • 23
  • 1
    Hi denistepp, I use `new Date().toISOString()` to store the timestamp for each message, and when i retrieve it them, it'd be something like `db.collection('chats').doc(chatId).collection('messages').orderBy('timestamp', 'desc').onSnapshot(...)`. With that said, I use my emulator and actual device to test, and gifted chat just sorts the messages according to my device/emulator time. – domster Jul 01 '21 at 09:26
  • 1
    yeah but it won't fix the problem if devices times are different. I found the solution by calculating the offset over the only one origin of time source. – denistepp Jul 01 '21 at 09:32
  • 1
    ahhhh i see... great to hear!! – domster Jul 01 '21 at 09:34

0 Answers0