What is the optimal way to perform and initial sync using MAM?

I’m working on migrating an app that had been based on Firebase to XMPP backed by Mongoose on the server side and using Smack (v 4.3.0) on the Android side. I am trying to build a chat list screen that looks something like facebook messenger’s seen here https://icdn2.digitaltrends.com/image/facebook-messenger-holiday-update-1-720x720.jpg?ver=1.jpg

Since I’m migrating, I have a lot of users with large roster lists and moderate message exchanges, so the initial sync performance is an issue. Currently once I get the roster I loop through all the entries and query for their most recent message, but if a user has 1000 members on their roster, that would be 1000 queries to the server, which is extremely slow. I already do prioritize one group of users (active) over the rest of the users (inactive) but if that active list is long it doesn’t make much of a difference.

I have considered updating the server to allow pages of 1000 messages to be sent back instead of 50 in which case I could just query a user’s entire message history. This would be faster in the case that the user has less than 1000 messages per person on their roster. However, there would be no guarantee that we would have a single message for each person in the roster without querying the entire history since one roster member could have sent only 1 message and it was the first message ever received. So this solution would be a lot of data use and could fill up a user’s phone pretty quickly if they don’t have a lot of disk space.

Is there any other way to populate a chat list screen that shows the most recent message? Ideally I want a query that is something similar to “return 1 message for each JID in the given JID list”.

I think your main issue is that you view the user’s archive on the server as many archives per corresponding JID, while it really is just a single archive which you want to synchronize your client’s copy from.

Yes, it is a single archive. But I want to synchronize specific portions for the performance, memory and network reasons to achieve the connections list screen.

How about:

  • synchronize the local archive, ideally starting from most recent messages to oldest
  • if the synchronization has never happened, additionally request the last n messages for the contacts per JID which are currently user visible

as soon as you have sufficient history synchronized, you can display the last messages per contact from your local archive.

I know that you requested a query like “give me the last n messages for this set of JIDs” in your stackoverflow post, but this is currently not possible with the current XEP. But, luckily, XMPP and the MAM XEP is extensible. It would be trivial to specify such query.

I am undecided if this is the right way, or if there is a better way. So I would like to hear what others say. However, this is not the right place to discuss this. Hence I would recommend you to bring it up on the standards@ mailing list.