Share via

Implementing startAfter functionality for paginated messages with Azure Communication Chat

Isaac Wesego 20 Reputation points
2024-06-26T12:18:48.7733333+00:00

Hello,

I have built a React application that uses @azure/communication-chat to fetch messages by page. I have used the following code to implement this:

const messages = chatThreadClient.listMessages({
  maxPageSize: pageSize,
});

const tst = messages.byPage();

However, I'm struggling to find a way to fetch the next page and subsequent pages. How can I implement fetching messages in chunks?

Thank you.

Azure Communication Services
0 comments No comments

Answer accepted by question author

SnehaAgrawal-MSFT 22,721 Reputation points Moderator
2024-07-02T16:31:56.88+00:00

@Isaac Wesego Apologies for late response, You may refer to quickstart documentation here: 

Quickstart - Add chat to your app - An Azure Communication Services quickstart | Microsoft Learn

 

The listMessages() returns a PagedAsyncIterableIterator:

 

/**
     * Gets a list of message from a thread identified by threadId.
     * Returns the list of the messages.
     * @param options - Get messages options.
     */
    listMessages(options?: ListMessagesOptions): PagedAsyncIterableIterator<ChatMessage>;

 

That has a next() method:

 

 /**
An interface that allows async iterable iteration both to completion and by page.
*/
export declare interface PagedAsyncIterableIterator<TElement, TPage = TElement[], TPageSettings = PageSettings> {
    /**
     * The next method, part of the iteration protocol
     */
    next(): Promise<IteratorResult<TElement>>;
    /**
     * The connection to the async iterator, part of the iteration protocol
     */
    Symbol.asyncIterator: PagedAsyncIterableIterator<TElement, TPage, TPageSettings>;
    /**
     * Return an AsyncIterableIterator that works a page at a time
     */
    byPage: (settings?: TPageSettings) => AsyncIterableIterator<TPage>;
}
   

The documentation for the core-paging module is here: azure-sdk-for-js/sdk/core/core-paging/README.md at main · Azure/azure-sdk-for-js (github.com)

Hope this helps, let us know,

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.