Message history
PubNub APIs let you effectively fetch historical messages from direct, group, or public conversations.
While App Context API lets you manage metadata and relationships between users and channels, enabling efficient tracking of which channels are associated with a given user, Message Persistence API lets you retrieve messages from those channels. Together, these APIs enable you to gather all conversations involving a user and easily fetch specific message histories for any interactions between users.
Chat SDK lets you fetch historical messages from one channel using the getHistory() method.
Due to current PubNub API limitations, you cannot filter the results by type, so you'll get all messages that happened on a given channel in a given timeframe.
Method signature
This method takes the following parameters:
1channel.getHistory(
2    startTimetoken: Timetoken? = nil,
3    endTimetoken: Timetoken? = nil,
4    count: Int = 25
5  ) async throws -> [MessageImpl]
Input
| Parameter | Description | 
|---|---|
| startTimetokenType:  TimetokenDefault: n/a | Timetoken delimiting the start of a time slice (exclusive) to pull messages from. For details, refer to the Fetch History section. | 
| endTimetokenType:  TimetokenDefault: n/a | Timetoken delimiting the end of a time slice (inclusive) to pull messages from. For details, refer to the Fetch History section. | 
| countType:  IntDefault: 25 | Number of historical messages to return for the channel in a single call. Since each call returns all attached message reactions by default, the maximum number of returned messages is 25. For more details, refer to the description of theincludeMessageActionsparameter in the Swift SDK docs. | 
Output
| Parameter | Description | 
|---|---|
| [MessageImpl] | A list of Messageobjects with pagination information (isMore), or anErrorif the request failed. | 
By default, each call returns all message reactions and metadata attached to the retrieved messages.
Sample code
Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
From the support channel, fetch 10 historical messages older than the timetoken 15343325214676133.
1// Assuming you have a reference of type "ChatImpl" named "chat"
2Task {
3  if let channel = try await chat.getChannel(channelId: "support") {
4    let response = try await channel.getHistory(startTimetoken: 15343325214676133, count: 10)
5    debugPrint("Messages: \(response.messages)")
6    debugPrint("Has more items to fetch: \(response.isMore)")
7    
8    // The code below demonstrates how to fetch the next page of messages. Skip it if you're only 
9    // interested in the initial set.
10
11    // Retrieves the timetoken of the oldest message from the previous response
12    let theOldestTimetoken = response.messages.compactMap { $0.timetoken }.min()
13    
14    // Fetch the next historical messages by using the timetoken of the oldest message from the previous response
15    // as the 'startTimetoken:' parameter for the subsequent call. Continue fetching in this way to retrieve