---
source_url: https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/messages/history
title: Message history
updated_at: 2026-06-16T12:48:53.933Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Message history

PubNub APIs let you effectively fetch historical messages from direct, group, or public conversations.

While [App Context API](https://www.pubnub.com/docs/general/metadata/basics) 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](https://www.pubnub.com/docs/general/storage) 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.

Use `getHistory()` to fetch past messages from a channel.

:::note API limitation
Results cannot be filtered by message type. All messages within the specified timeframe are returned.
:::

### Method signature

This method takes the following parameters:

```swift
channel.getHistory(
    startTimetoken: Timetoken? = nil,
    endTimetoken: Timetoken? = nil,
    count: Int = 25
  ) async throws -> (messages: [MessageImpl], isMore: Bool)
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| startTimetoken | Timetoken | Optional |  | [Timetoken](https://www.pubnub.com/docs/sdks/swift/api-reference/misc#time) delimiting the start of a time slice (exclusive) to pull messages from. For details, refer to the [Fetch History section](https://www.pubnub.com/docs/sdks/swift/api-reference/storage-and-playback#fetch-history). |
| endTimetoken | Timetoken | Optional |  | Timetoken delimiting the end of a time slice (inclusive) to pull messages from. For details, refer to the [Fetch History section](https://www.pubnub.com/docs/sdks/swift/api-reference/storage-and-playback#fetch-history). |
| count | Int | Optional | `25` | Number of historical messages to return for the channel in a single call. Since each call returns all attached [message reactions](https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/messages/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](https://www.pubnub.com/docs/sdks/swift/api-reference/storage-and-playback#methods). |

#### Output

| Parameter | Description |
| --- | --- |
| `(messages: [MessageImpl], isMore: Bool)` | A tuple containing a list of `MessageImpl` objects and a boolean flag indicating if there are more messages available. |

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

### Sample code

:::tip 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`.

```swift
// Assumes a "ChatImpl" reference named "chat"
Task {
  if let channel = try await chat.getChannel(channelId: "support") {
    let response = try await channel.getHistory(startTimetoken: 15343325214676133, count: 10)
    debugPrint("Messages: \(response.messages)")
    debugPrint("Has more items to fetch: \(response.isMore)")
      
    let theOldestTimetoken = response.messages.compactMap { $0.timetoken }.min()
      
    if let theOldestTimetoken, response.isMore {
      let nextHistoryResults = try await channel.getHistory(startTimetoken: theOldestTimetoken, count: 10)
    }
  } else {
    debugPrint("Channel not found")
  }
}
```