[CHAT SDK] [Help Needed] Best Practices for Paginating Messages in Chat SDK
Description
I'm developing an Android app using the Azure Communication Services Chat SDK and facing difficulties with implementing proper pagination for chat messages in a thread.
When attempting to retrieve chat messages in chunks (e.g., 50 or 100 messages at a time) instead of loading all messages at once, I'm experiencing unexpected behaviour. My goal is to:
- Load the most recent N messages initially
- Allow users to load older messages in chunks when they request more
- Prevent excessive memory usage and improve performance by not loading all messages at once
Current Implementation
I'm using ListChatMessagesOptions.setMaxPageSize() to set the page size, but when I call listMessages() with these options, it appears to fetch all pages at once rather than allowing me to fetch them one at a time as needed.
// Setting up pagination options
ListChatMessagesOptions listMessagesOptions = new ListChatMessagesOptions()
.setMaxPageSize(PAGE_SIZE); // e.g., PAGE_SIZE = 50
// Attempting to load messages with pagination
chatThreadAsyncClient.listMessages(listMessagesOptions, null)
.byPage()
.forEach(page -> {
// Process page
continuationToken = page.getContinuationToken();
// ...
});
Questions
-
Is this the correct approach for implementing pagination? My understanding was that
setMaxPageSize()should limit each page to N messages, but it seems the SDK is still fetching all pages at once. -
What is the recommended pattern for implementing "load more" functionality? I'd like to:
- Load the most recent messages first
- Store a continuation token
- Load older messages when the user requests them
-
Can you provide a code example for proper pagination implementation? Specifically, how to:
- Load initial page of messages (most recent)
- Store the continuation token properly
- Use that token to load previous/older messages when needed
Environment
- Android App
- Azure Communication Services Chat SDK Version: 1.0.0
- Azure Communication Services Common SDK Version: 1.0.1
- Minimum Android SDK: 26
- Target Android SDK: 33
Additional Context
This functionality is critical for our application as chat threads can potentially contain thousands of messages, and loading them all at once causes performance issues and a poor user experience.
Any guidance or examples would be greatly appreciated!