---
source_url: https://www.pubnub.com/docs/chat/sdks/messages/message-history
title: Message history (deprecated)
updated_at: 2026-05-22T11:04:25.433Z
---

> 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 (deprecated)

:::warning Use Chat SDKs
This documentation is deprecated. Use any of our dedicated [Chat SDKs](https://www.pubnub.com/docs/chat/overview) to quickly implement chat functionality in your application.
:::

PubNub allows you to store and retrieve messages as they get sent over the network by using the [Message Persistence](https://www.pubnub.com/docs/general/storage) feature. PubNub uses a time-series based database in which each message is stored on the channel it was published, timestamped to the nearest 10 nanoseconds. The message retention policy can be configured from your account with the following options: 1 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.

:::note User ID / UUID
User ID is also referred to as **UUID/uuid** in some APIs and server responses but **holds the value** of the **userId** parameter you [set during initialization](https://www.pubnub.com/docs/general/setup/users-and-devices#set-the-user-id).
:::

## Fetch messages from channels

Use the [fetchMessages](https://www.pubnub.com/docs/sdks/javascript/api-reference/storage-and-playback#fetch-history) method to fetch messages from Message Persistence on one or more channels. This method returns messages that were published before the `start` timetoken and after the `end` timetoken.

### Start and end parameters

Messages are always returned in a chronological order—from oldest to newest—within the timetoken range you request.

If you specify only the `start` parameter (without `end`), you will receive messages *older than the start timetoken value*.

If you specify only the `end` parameter (without `start`), you will receive messages *from the last (most recent) message going back to that timetoken value*.

Specify values for both `start` and `end` to retrieve messages *between* those timetokens (inclusive of the end value).

:::tip Pagination
In a single request, you can set the `count` parameter for the `fetchMessages` method to return up to 100 messages on a single channel and 25 messages per channel on a maximum of up to 500 channels. If you need to fetch more messages from Message Persistence, send another request by setting the `start` timetoken to the oldest received in the previous request.
:::

###### JavaScript

```js
// start, end, count are optional
pubnub.fetchMessages(
    {
        channels: ['ch-1'],
        end: '15343325004275466',
        count: 100
    },
    (status, response) => {
        // handle response
    }
);
```

###### Swift

```swift
pubnub.fetchMessageHistory(
  for: ["ch-1"],
  end: 15343325004275466,
  max: 100
) { result in
  switch result {
  case let .success(response):
    print("Successful History Fetch Response: \(response)")
  case let .failure(error):
    print("Failed History Fetch Response: \(error.localizedDescription)")
  }
}
```

###### Java

```java
pubnub.fetchMessages()
        .channels(Arrays.asList("ch-1"))
        .end(15343325004275466)
        .maximumPerChannel(100)
        .async(result -> {
            result.onSuccess( res -> {
                    Map<String, List<PNFetchMessageItem>> channels = res.getChannels();
                for (PNFetchMessageItem messageItem : channels.get("my_channel")) {
                    System.out.println(messageItem.getMessage());
                    System.out.println(messageItem.getMeta());
                    System.out.println(messageItem.getTimetoken());
                    Map<String, Map<String, List<PNFetchMessageItem.Action>>> actions =
                            messageItem.getActions();
                    for (String type : actions.keySet()) {
                        System.out.println("Action type: " + type);
                        for (String value : actions.get(type).keySet()) {
                            System.out.println("Action value: " + value);
                            for (PNFetchMessageItem.Action action : actions.get(type).get(value)) {
                                System.out.println("Action timetoken: " + action.getActionTimetoken());
                                System.out.println("Action publisher: " + action.getUuid());
                            }
                        }
                    }
                }
            }).onFailure( exception -> {
                    exception.printStackTrace();
            });
        });
```

###### Unity

```csharp
pubnub.FetchMessages()
      .Channels(new List<string>{"ch-1"})
      .End(15343325004275466)
      .Count(100)
      .Async ((result, status) => {
              if(status.Error){
                Debug.Log (string.Format(" FetchMessages Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
              } else {
                Debug.Log (string.Format("In FetchMessages, result: "));
                foreach(KeyValuePair<string, List<PNMessageResult>> kvp in result.Channels){
                  Debug.Log("kvp channelname" + kvp.Key);
                  foreach(PNMessageResult pnMessageResut in kvp.Value){
                    Debug.Log("Channel: " + pnMessageResut.Channel);
                    Debug.Log("payload: " + pnMessageResut.Payload.ToString());
                    Debug.Log("timetoken: " + pnMessageResut.Timetoken.ToString());
                  }
                }
              }
      });
```

## Fetch messages with reactions

Use the [fetchMessages](https://www.pubnub.com/docs/sdks/javascript/api-reference/storage-and-playback#fetch-history) method with the `includeMessageActions` parameter set to fetch past messages in a channel along with reactions that were added to those messages. You can only fetch messages from a single channel at a time.

:::note Count limits
While fetching messages with reactions, the `count` parameter has a max value of 25.
:::

###### JavaScript

```js
// start, end, count are optional
const response = await pubnub.fetchMessages({
    channels: ['ch-1'],
    includeMessageActions: true,
    end: '15343325004275466',
    count: 25
 });
```

###### Swift

```swift
pubnub.fetchMessageHistory(
  for: ["ch-1"],
  includeActions: true,
  page: PubNubBoundedPageBase(end: 15343325004275466, limit: 25)
) { result in
  switch result {
  case let .success(response):
    print("Successful History Fetch Response: \(response)")
  case let .failure(error):
    print("Failed History Fetch Response: \(error.localizedDescription)")
  }
}
```

###### Java

```java
pubnub.fetchMessages()
        .channels(Arrays.asList("ch-1"))
        .includeMessageActions(true)
        .end(15343325004275466L)
        .maximumPerChannel(25)
        .async(result -> {
            result.onSuccess(res -> {
                Map<String, List<PNFetchMessageItem>> channels = res.getChannels();
                for (PNFetchMessageItem messageItem : channels.get("my_channel")) {
                    System.out.println(messageItem.getMessage());
                    System.out.println(messageItem.getMeta());
                    System.out.println(messageItem.getTimetoken());
                    Map<String, Map<String, List<PNFetchMessageItem.Action>>> actions =
                            messageItem.getActions();
                    for (String type : actions.keySet()) {
                        System.out.println("Action type: " + type);
                        for (String value : actions.get(type).keySet()) {
                            System.out.println("Action value: " + value);
                            for (PNFetchMessageItem.Action action : actions.get(type).get(value)) {
                                System.out.println("Action timetoken: " + action.getActionTimetoken());
                                System.out.println("Action publisher: " + action.getUuid());
                            }
                        }
                    }
                }
            }).onFailure(exception -> {
                exception.printStackTrace();
            });
        });
```

###### Unity

```csharp
pubnub.FetchMessages()
      .Channels(new List<string>{"ch-1"})
      .IncludeMessageActions(true)
      .End(15343325004275466)
      .Count(25)
      .Async ((result, status) => {
              if(status.Error){
                Debug.Log (string.Format(" FetchMessages Error: {0} {1} {2}", status.StatusCode, status.ErrorData, status.Category));
              } else {
                Debug.Log (string.Format("In FetchMessages, result: "));
                foreach(KeyValuePair<string, List<PNMessageResult>> kvp in result.Channels){
                  Debug.Log("kvp channelname" + kvp.Key);
                  foreach(PNMessageResult pnMessageResut in kvp.Value){
                    Debug.Log("Channel: " + pnMessageResut.Channel);
                    Debug.Log("payload: " + pnMessageResut.Payload.ToString());
                    Debug.Log("timetoken: " + pnMessageResut.Timetoken.ToString());
                  }
                }
              }
      });
```

For more details on working with message history, refer to [Fetching Messages from Message Persistence](https://www.pubnub.com/docs/general/storage).