[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.
6 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
good question. i would start off with two queries, and you can rearrange to share code later. One query does
and the other does
Then you call
usePaginatedQuery
twice, once on each query. the first one is [LAST_MESSAGE_SEEN, ...OLD_SEEN_MESSAGES]
(ordered from newer to older), with the loadMore
function loading older seen messages. The second one is [...NEW_UNREAD_MESSAGES]
(ordered from older to newer), with the loadMore
function loading newer unread messagesTake a look on getPage, https://stack.convex.dev/pagination
Take Control of Pagination
Convex offers robust control over pagination with a powerful function, getPage, enabling complex edge cases. In this article, we go over how to use th...
I did something like what you mention here:
https://github.com/jamalsoueidan?tab=repositories
Scroll down to get the latest messages just like whatsapp.
GitHub
jamalsoueidan - Repositories
Fullstack is what I do! and this is my profile. jamalsoueidan has 53 repositories available. Follow their code on GitHub.
Ooooh lovely, thanks @jamalsoueidan and @lee !!
You welcome, I think their was article on convex that show you exactly what you want, like get from the middle, and scroll up and down to get more data, but i dont remember where its located 🔥