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.
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.
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:
1// Get messages from the newest to oldest2channel3.getMessages(20 /* by pages of 20 messages */)4.then(function(messagesPage) {5messagesPage.items.forEach(function(message) {6// do stuff for each message7});8// note, that by default pages are in "backwards" order,9// so you should ask for previous page, not the next one10if (messagesPage.hasPrevPage) {11return messagesPage.prevPage(); // here we can ask for following page12}13});1415// Get messages from the message with id 3 to newest16channel.getMessages(20, 3, 'forward').then(function(messagesPage) {17/* handle page the same way as above */18});1920// Handle multiple pages21// Unfortunately, js has no asynchronous iterators yet, so there should be22// some boilerplate here. One of the way to go through all messages would be23// like this:24function processPage(page) {25page.items.forEach(function(message) {26/* do something for message */27});28if (page.hasNextPage) {29page.nextPage().then(processPage);30} else {31// all messages read!32}33}34channel.getMessage(10, null, 'forward').then(processPage);
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.1// Fetch the initial messages2messagesObject.getLastMessages(BATCH_SIZE, new CallbackListener<List<Message>>()3{4@Override5public void onSuccess(List<Message> messagesArray) {6// Display initial messages in your UI7}8});910// ... user scrolls through messages list1112// Fetch next set of messages when you get close to the13// end of your local messages14messagesObject.getMessagesBefore(firstMessage.getMessageIndex(), BATCH_SIZE,15new CallbackListener<List<Message>>()16{17@Override18public void onSuccess(List<Message> messagesArray) {19// Display latest messages in your UI20}21});
Next: Roles and Permissions