PierreP
Convex Community14mo ago
6 replies
Pierre

[Paginated Query] How to get the first elements in the "middle"

Context: Let's say we build a chat in a mobile app: it consists of a screen that displays messages vertically.
In order to achieve this, it makes sense to have a paginated query where messages are ordered by
_creationTime
.
Let's say a user is off for one week, have hundreds of missing messages, and open the chat. A common feature is to display the last seen message and then the new ones, and to scroll down until the most recent one.

Problem:
How do we do to "start" the paginated query so that it returns items started from the LAST_MESSAGE_SEEN and can scroll up or down to load more?
We could load of course all the messages until the LAST_MESSAGE_SEEN , and scroll to it, but that wouldn't scale much if hundred of messages were not read, and wouldn't give the chat a snappy feeling.

// Conceptually, the message list would look like this
[...OLD_SEEN_MESSAGES, LAST_MESSAGE_SEEN, ...NEW_UNREAD_MESSAGES]
Was this page helpful?