0

Have created a chat application and we use firebase for realtime communication. Sometimes i noticed that push() method shuffle the list data. We can see in the below image :

enter image description here

If we see in above image i am just trying to communicate with someone i said hello in reply user said hey, again i said i need some help then user said That's what I'm here for. What can I assist you with? in the reply but if we see in the image users reply is appearing first.

It happens intermittently that's why i didn't figure out this problem. So please someone help me out what shall i doing wrong.

var pushMessageToFB = function(){
var chatMsgRef = db.child("chatMessages").child("gr1").child("ch_usr1_usr2");
var message = {
    type: "chat",
    content: data.messageText,
    timestamp: Date.now(),
    by: user.id
};
chatMsgRef.push(message, function(err){
    if (err){
        console.log('error occurred while pushing message to fb : err ' + JSON.stringify(err));
    }
});};
var loadChatMessages = function(){
var chatMsgRef = db.child('chatMessages').child("gr1").child("ch_usr1_usr2");
$scope.chatMessages = chatMsgRef.orderByKey().limitToLast(50);  

};

  • Please provide your relevant code and data structure, otherwise there's nothing to diagnose. Specifically how you're writing, querying, and displaying the replies. – Travis Christian Mar 01 '17 at 19:26
  • Hi Travis Christian...as i said earlier that it happens sometimes not regularly that's why i am not able to figure out this problem – Gaurav Priyadarshi Mar 01 '17 at 19:39

1 Answers1

2

Don't use Date for remote data. It will be slightly offset on every machine. Use ServerValue.TIMESTAMP which Firebase will convert to the server's Unix epoch time when the data is written. This ensures consistency of order across clients.

You didn't provide any code for how you're reading or displaying the chat messages, but since you're trying to show them in chronological order I assume your query uses orderBy("timestamp"). If you use ServerValue.TIMESTAMP when you write the message it will be guaranteed to sort in the order that it was written to the database.

Community
  • 1
  • 1
Travis Christian
  • 2,412
  • 2
  • 24
  • 41
  • 2
    As crazy as it sounds, I wouldn't use TIMESTAMP for this either. At Firetalk we (I) saw one or two cases in high-volume live chats that timestamps could intersect. It seems unlikely but it's absolutely not impossible. Firebase's auto-gen ID is usually the best option here to sort on. It was specifically designed for this purpose. – Chad Robinson Mar 02 '17 at 05:54
  • Hi Chad Robinson, I use orderByKey(). Here is the code var loadChatMessages = function(){ var chatMsgRef = db.child('chatMessages').child("gr1").child("ch_usr1_usr2"); $scope.chatMessages = chatMsgRef.orderByKey().limitToLast(50); }; – Gaurav Priyadarshi Mar 02 '17 at 06:25
  • @ChadRobinson thanks for that, I wasn't sure if push keys were semantically in strict chronological order since I have some old items in my DB that are shown at the bottom of the collection despite being alphanumerically lesser. Is that just an artifact of the SDK upgrade? – Travis Christian Mar 02 '17 at 15:46
  • @GauravPriyadarshi if this doesn't solve your problem, [edit your question](https://stackoverflow.com/posts/42540252/edit) to include the relevant code and a sample of your data. – Travis Christian Mar 02 '17 at 15:47
  • @TravisChristian Firebase docs say `When working with lists of data push() ensures a unique and chronological key.` Sorry, I don't have a lot of experience with older SDK versions and that may be true, but at least for latest implementations, this is their official position. – Chad Robinson Mar 02 '17 at 20:51