Handling pagination

This guide covers the process loading and displaying chunks of conversation events at a time.

NOTE: A step-by-step tutorial to build a chat application is available here.

Given a Conversation you are already a Member of, you can download chunks (pages) of events existing within this conversation.

Load first page of events:

function listEvents(events_page){
      events_page.items.forEach(event => {
        handleEvent(event);
      })
}

conversation.getEvents({ page_size: 100, order: 'asc' })
    .then((events_page) => {
        listEvents(events_page);
    })
    .catch((error) =>{
        console.log('Error: Unable to load conversation events ',error);
    });
val conversationEventsListener = object : NexmoRequestListener<NexmoEventsPage> {
    override fun onSuccess(nexmoEventsPage: NexmoEventsPage?) {
        nexmoEventsPage?.pageResponse?.data?.let {
            val events = nexmoEventsPage?.data
        }
    }

    override fun onError(apiError: NexmoApiError) {
        Log.d("TAG", "Error: Unable to load conversation events ${apiError.message}")
    }
}

conversation?.getEvents(100, NexmoPageOrder.NexmoMPageOrderAsc, null, conversationEventsListener)
NexmoRequestListener<NexmoEventsPage> conversationEventsListener = new NexmoRequestListener<NexmoEventsPage>() {
    @Override
    public void onSuccess(@Nullable NexmoEventsPage eventsPage) {
        Collection<NexmoEvent> events = eventsPage.getData();
    }

    @Override
    public void onError(@NonNull NexmoApiError apiError) {
        Log.d("TAG", "Error: Unable to load conversation events %s", apiError.getMessage());
    }
};

conversation.getEvents(100, NexmoPageOrder.NexmoMPageOrderAsc, null, conversationEventsListener);
conversation.getEventsPage(withSize: 10, order: .asc) { (error, page) in

    if let error = error {
        print(error)
        return
    }

    self.events = page?.events
}
[self.conversation getEventsPageWithSize:10 order:NXMPageOrderAsc
 completionHandler:^(NSError * _Nullable error, NXMEventsPage * _Nullable events) {

    self.events = [NSMutableArray arrayWithArray:events.events];

}];

After loading the first chunk of events you will get the reference to the current Nexmo Events Page (JavaScript, iOS, Android). This reference allows you to retrieve following event pages:

if (events_page.hasNext()){
    listEvents(events_page.getNext());
}
if(nexmoEventsPage.isNextPageExist) {
    nexmoEventsPage.getNext(conversationEventsListener)
}
if(eventsPage.isNextPageExist()) {
    eventsPage.getNext(conversationEventsListener);
}
if eventsPage.hasNextPage() {
    eventsPage.nextPage({ (error, page) in
        // handle page events
    })
}
if (eventsPage.hasNextPage) {
    [eventsPage nextPage:^(NSError * _Nullable error, NXMEventsPage * _Nullable page) {
        // handle page events
    }];
}

Preceding pages can also be retrieved using a similar technique:

if (events_page.hasPrev()){
    listEvents(events_page.getPrev());
}
if(nexmoEventsPage.isPrevPageExist) {
    nexmoEventsPage.getPrev(conversationEventsListener)
}
if(eventsPage.isPrevPageExist()) {
    eventsPage.getPrev(conversationEventsListener);
}
if eventsPage.hasPreviousPage() {
    eventsPage.previousPage({ (error, page) in
        // handle page events
    })
}
if (eventsPage.hasPreviousPage) {
    [eventsPage previousPage:^(NSError * _Nullable error, NXMEventsPage * _Nullable page) {
        // handle page events
    }];
}