Pagination with the Vonage Client SDK
When getting a list of objects with the Vonage Client SDK, the response will be paginated. This means that rather than returning all the objects at once, which would increase latency and the size of the response, you will get a chunk of the response or a page. For example, calling getConversations will return a ConversationsPage.
Functions that return a page, will have parameters for a page size, and a cursor. By supplying a cursor you can traverse the pages until you get all the results. Here is an example with getting Conversations.
let conversations, nextCursor, previousCursor
const params = {
order: "asc", // "desc"
pageSize: 5,
cursor: null,
includeCustomData: true,
orderBy: null // "CUSTOM_SORT_KEY"
};
client.getConversations(params)
.then(conversationsPage => {
({conversations, nextCursor, previousCursor} = conversationsPage);
console.log("Array of Conversations: ", conversations);
console.log("Cursor for next set of results, if any. Could be null: ", nextCursor);
console.log("Cursor for previous set of results, if any. Could be null: ", previousCursor);
}).catch(error => {
console.error("Error getting Conversations: ", error);
});
val params = GetConversationsParameters(order = PresentingOrder.ASC, pageSize = 5, cursor = null)
client.getConversations(params) { error, conversationsPage ->
error?.let { /* Handle Error in fetching Conversations */ }
conversationsPage?.let {
val nextCursor = it.nextCursor
val previousCursor = it.previousCursor
it.conversations.forEach {conversation ->
println("Conversation id: ${conversation.id}, name: ${conversation.name}")
}
}
}
let params = VGGetConversationsParameters(order: .asc, pageSize: 5, cursor: nil)
client.getConversations(params) { error, conversationsPage in
if error == nil {
let conversations = conversationsPage!.conversations
let nextCursor = conversationsPage!.nextCursor
let previousCursor = conversationsPage!.previousCursor
} else {
// Handle failure
}
}
On the ConversationsPage there is a nextCursor and previousCursor parameter. To get the next page of Conversations, call getConversations again, but this time provide the value from the current ConversationsPage.nextCursor for the cursor:
const params = {
order: "asc", // "desc"
pageSize: 5,
cursor: nextCursor, // previousCursor
includeCustomData: true,
orderBy: null // "CUSTOM_SORT_KEY"
};
client.getConversations(params)
...
val params = GetConversationsParameters(order = PresentingOrder.ASC, pageSize = 5, cursor = nextCursor)
client.getConversations(params) { error, conversationsPage ->
error?.let { /* Handle Error in fetching Conversations */ }
conversationsPage?.let {
it.conversations.forEach {conversation ->
println("Conversation id: ${conversation.id}, name: ${conversation.name}")
}
}
}
let params = VGGetConversationsParameters(order: .asc, pageSize: 5, cursor: nextCursor)
client.getConversations(params) { error, conversationsPage in
...
}
When you reach the end of the results, nextCursor will be nil/null. Inversely, when you reach the start of the results previousCursor will be nil/null.