This interface allows you to get messages from the end of the conversation one page at a time. This allows you to implement the classic pull down to load more interface.
This method uses a continuation token manage the paging. If you want to enumerate from the end, you don't specify a token. As part of the response, a continuation token is returned along with the messages. This is fed back in the next time we want to query for messages. If the wrong token is specified, the method returns an error and you have to go back to the end of the list of messages.
Example
// Call the getMessages() api for the first time
sdk.services.appMessaging.getMessages("CA29B56B-30D6-4217-9C99-577AA7525B92", 100 )
.then(response => {
console.log("getMessages() succeeded", response);
console.log("Here are your messages ...", response.messages);
console.log("Here is your continuation token, store this and use the next time you call this method", response.continuationToken);
})
.catch(response => {
console.error("getMessages() failed", error);
});
To query the previous page of messages, you need to feed in the continuation token that was returned the last time you queried this conversation. The token is specific to this conversation only.
// Call the getMessages() api for the second time
//
sdk.services.appMessaging.getMessages("CA29B56B-30D6-4217-9C99-577AA7525B92", 100, continuationToken )
.then(response => {
console.log("getMessages() succeeded", response);
console.log("Here are your messages ...", response.messages);
console.log("Here is an updated continuation token", response.continuationToken);
})
.catch(response => {
console.error("getMessages() failed", error);
});
You can repeat this until you reach the beginning of the conversation. When the continuation token returned is <= 0
, you have all the messages.
Here’s the definition of the response returned through a promise from the call to getMessages()
:
export interface IGetMessagesResponse {
continuationToken?: number;
earliestEventId?: number;
latestEventId?: number;
messages: IConversationMessage[];
}
The eventId
's are used later when we handle incoming events from the WebSocket.
The foundation SDK doesn't deal with any kind of conversation persistence; this is up to you manage. You might choose to just query messages from the end of the conversation each time, without persisting anything.
Apply real-time events to conversation messages stored locally
Whether you choose to store your messages in a database, or just in-memory, you must have a way to deal with new events that arrive.
These events take the form of new messages and messages being marked as delivered or read.
Learn more in WebSocket events.