Skip to contentSkip to navigationSkip to topbar
Rate this Page:

Result Paging in Chat Client SDKs


(error)

Danger

Programmable Chat has been deprecated and is no longer supported. Instead, we'll be focusing on the next generation of chat: Twilio Conversations. Find out more about the EOL process here(link takes you to an external page).

If you're starting a new project, please visit the Conversations Docs to begin. If you've already built on Programmable Chat, please visit our Migration Guide to learn about how to switch.

Twilio's Programmable Chat client SDKs use paging to improve performance when accessing potentially large collections of chat objects.


Paginators

paginators page anchor

Public Channels, User Channels, and the Members list for each channel are exposed through Paginators. In JavaScript, Messages are also paged using Paginators. When requesting an initial set of any of these entities, your provided callback mechanism will receive a result indicating the success or failure of the operation as well as a paginator to access the items.

While the signature of the individual methods will vary by SDK platform, each paginator has the following accessors:

  • A way to obtain the items.
  • A boolean property indicating if there are subsequent pages.
  • A method taking the same callback mechanism as the original call to request the next page.

Result Paging: Obtain a List of Public Channel Descriptors

result-paging-obtain-a-list-of-public-channel-descriptors page anchor
Node.js
Java
Objective-C
Swift

_34
// Get messages from the newest to oldest
_34
channel
_34
.getMessages(20 /* by pages of 20 messages */)
_34
.then(function(messagesPage) {
_34
messagesPage.items.forEach(function(message) {
_34
// do stuff for each message
_34
});
_34
// note, that by default pages are in "backwards" order,
_34
// so you should ask for previous page, not the next one
_34
if (messagesPage.hasPrevPage) {
_34
return messagesPage.prevPage(); // here we can ask for following page
_34
}
_34
});
_34
_34
// Get messages from the message with id 3 to newest
_34
channel.getMessages(20, 3, 'forward').then(function(messagesPage) {
_34
/* handle page the same way as above */
_34
});
_34
_34
// Handle multiple pages
_34
// Unfortunately, js has no asynchronous iterators yet, so there should be
_34
// some boilerplate here. One of the way to go through all messages would be
_34
// like this:
_34
function processPage(page) {
_34
page.items.forEach(function(message) {
_34
/* do something for message */
_34
});
_34
if (page.hasNextPage) {
_34
page.nextPage().then(processPage);
_34
} else {
_34
// all messages read!
_34
}
_34
}
_34
channel.getMessage(10, null, 'forward').then(processPage);


Messages (Android, iOS)

messages-android-ios page anchor

The messages collection behaves a bit differently than channels and members since there is a temporal quality to how the objects are typically presented.

The messages collection objects offer the following ways to access items:

  • getLastMessages fetches the specified number of messages, starting with the most recent in the collection.
  • getMessagesBefore fetches messages before (and including) the anchor message index specified.
  • getMessagesAfter fetches messages after (and including) the anchor message index specified.
  • messageWithConsumptionIndex fetches the message with the specified index or, if that message is no longer available, the message directly before it.
  • messageWithIndex fetches the message specified by the index, if available.

Result Paging: Messages

result-paging-messages page anchor
Java
Objective-C
Swift

_21
// Fetch the initial messages
_21
messagesObject.getLastMessages(BATCH_SIZE, new CallbackListener<List<Message>>()
_21
{
_21
@Override
_21
public void onSuccess(List<Message> messagesArray) {
_21
// Display initial messages in your UI
_21
}
_21
});
_21
_21
// ... user scrolls through messages list
_21
_21
// Fetch next set of messages when you get close to the
_21
// end of your local messages
_21
messagesObject.getMessagesBefore(firstMessage.getMessageIndex(), BATCH_SIZE,
_21
new CallbackListener<List<Message>>()
_21
{
_21
@Override
_21
public void onSuccess(List<Message> messagesArray) {
_21
// Display latest messages in your UI
_21
}
_21
});

Next: Roles and Permissions


Rate this Page: