简体   繁体   中英

Optimize Firestore real time updates in chat app

I'm using Firestore real time updates to create realtime chats in my React Native app. I read this may not be the best way to build a chat, but I decided to do so cause I'm using Firebase already and the chat is not the main purpose of the app. So, in the context of a real time chat, how would I optimize the Firestore connection? It usually works really well but I have experienced a few problems so far:

  • message comes in slowly
  • message doesn't show after being sent
  • Push notification arrives before the message

These problems usually occur when internet connection is not great (though Whatsapp messages still work fine), but sometimes also on a good connection…

Here is how I query the data (real-time listener added in componentDidMount , removed in componenWillUnmount ):

onLogUpdate = (querySnapshot) => {
  allMessages = this.state.allMessages

  cb = (allMsgs) => {
    allMsgs.sort((a,b) => {
      a = new Date(a.timestamp);
      b = new Date(b.timestamp);
      return a>b ? -1 : a<b ? 1 : 0;
    })
    messages = _.takeRight(allMsgs, this.state.messageLimit)
    this.setState({ loading: false, messages, allMessages: allMsgs })
  }

  async.map(querySnapshot._changes, (change, done) => {
    if (change.type === "added") {
      const msgData = change._document.data()
        if (msgData.origin == 'user') {
          this.usersRef.doc(msgData.byWhom).get()
            .then(usr => {
              msgData.user = usr.data()
              done(null, msgData)
            })
            .catch(err => { console.log('error getting user in log:', err) })
        } else {
          done(null, msgData)
        }
    } else {
      done(null, 0)
    }
  }, (err, results) => {
    const res = results.filter(el => { return el != 0 })
    allMessages = _.concat(allMessages, res)
    cb(allMessages)
  })
}

And this is how I add new messages:

// in utils.js
exports.addLogMessage = (msgObj, moment_id, callback) => {
  logMessagesRef.add(msgObj)
    .then(ref => {
      momentsRef.doc(moment_id).get()
        .then(doc => {
          const logMsgs = doc.data().logMessages ? doc.data().logMessages : []
          logMsgs.push(ref.id)
          momentsRef.doc(moment_id).update({ logMessages: logMsgs })
        })
        .then(() => {
          if (callback) {
            callback()
          }
        })
    })
    .catch(err => { console.log('error sending logMessage:', err) })
}

// in chat Screen
sendLogMessage = () => {
  if (this.state.newMessage.length > 0) {
    firebase.analytics().logEvent('send_log_message')
    this.setState({ newMessage: '' })
    const msgObj = {
      text: this.state.newMessage,
      origin: 'user',
      timestamp: Date.now(),
      byWhom: this.state.user._id,
      toWhichMoment: this.state.moment._id
    }
    addLogMessage(msgObj, this.state.moment._id)
  }
}

Any suggestions would be highly appreciated :)

I was working on something similar and after i submitted the data it wouldnt show in the ListView. I solved it by pushing the new entry into the preexisting state that was being mapped, and since setState() calls the render() method, it worked just fine. I did something like this:

 sendLogMessage().then(newData => {
     joined = this.state.data.concat(newData);
     this.setState({ data: joined})
 })

This is ofcourse assuming that you're using this.state.data.map to render the chat log. This will work when the message that I send cant be seen by me and as for the messages updating as the database updates, you may wanna make use of .onDataChange callback provided by Firebase API . I hope I helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM