Message history

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:

channel.getHistory(
startTimetoken: Timetoken? = nil,
endTimetoken: Timetoken? = nil,
count: Int = 25,
completion: ((Swift.Result<[MessageImpl], Error>) -> Void)? = nil
)

Input

ParameterTypeRequiredDefaultDescription
startTimetokenTimetokenNon/aTimetoken delimiting the start of a time slice (exclusive) to pull messages from. For details, refer to the Fetch History section.
endTimetokenTimetokenNon/aTimetoken delimiting the end of a time slice (inclusive) to pull messages from. For details, refer to the Fetch History section.
countIntNo25Number 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 the includeMessageActions parameter in the Swift SDK docs.

Output

TypeDescription
[MessageImpl]A list of Message objects with pagination information (isMore), or an Error if the request failed.

By default, each call returns all message reactions and metadata attached to the retrieved messages.

Basic usage

From the support channel, fetch 10 historical messages older than the timetoken 15343325214676133.

/// Reference the previously created "Channel" object
chat.getChannel(channelId: "support") { [weak self] in
switch $0 {
case let .success(channel):
self?.channel = channel
case let .failure(error):
debugPrint("Error while getting a channel: \(error)")
}
}

var channel: ChannelImpl?

/// Invoke the "getHistory()" method
channel?.getHistory(endTimetoken: 15343325214676133, count: 10) {
switch $0 {
show all 22 lines
Last updated on