PubNub Logo Docs
Support Contact Sales Login Try Our APIs

›MESSAGES

Collapse all
Dark mode

Back to Home

Overview

  • In-App Chat

Chat Components

  • Overview
  • REACT

    • React Components

    ANDROID

    • Getting Started
    • UI Components
    • UI Theming
    • Data Components
    • Chat Provider
    • Message Reactions
    • Message Menu

    IOS

    • Getting Started
    • UI Components
    • UI Theming
    • Data Components
    • Chat Provider

SDKs

  • Overview
  • USERS

    • Setup
    • Metadata
    • Permissions
    • Presence
    • Mentions

    CHANNELS

    • Types and Names
    • Metadata
    • Subscriptions
    • Memberships

    MESSAGES

    • Sending Messages
    • Message History
    • Unread Counts
    • File Upload
    • Typing Indicators
    • Read Receipts
    • Emoji Reactions
    • Update Messages
    • Delete Messages
    • External Storage

    PUSH NOTIFICATIONS

    • Overview

    MODERATION

    • Profanity Filters
    • Flag Messages
    • Ban Users
    • Mute Users
    • Spam Prevention

    INTEGRATIONS

    • Overview
    • Content Moderation
    • Image Moderation
    • Language Translation
    • Chatbots
    • GIFs
    • Stickers

Moderation Dashboard

  • Overview
  • Getting Started
  • FEATURES

    • Automatic Text Moderation
    • Automatic Image Moderation
    • Manual Message Moderation
    • Manual User Moderation
    • User Management
    • Channel Management
  • Required Configuration

Debug Console
Network Status

Message history

PubNub allows you to store and retrieve messages as they get sent over the network by using the Storage and Playback 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, 3 days, 7 days, 15 days, 1 month, and Forever.

Fetch messages from channels

Use the fetchMessages method to fetch messages from storage 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).

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 storage, send another request by setting the start timetoken to the oldest received in the previous request.

JavaScript
Swift
Java
Unity

Go to SDK

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

Go to SDK

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)")
}
}

Go to SDK

pubnub.fetchMessages()
.channels(Arrays.asList("ch-1"))
.end(15343325004275466)
.maximumPerChannel(100)
.async(new PNCallback<PNFetchMessagesResult>() {
@Override
public void onResponse(PNFetchMessagesResult result, PNStatus status) {
if (!status.isError()) {
Map<String, List<PNFetchMessageItem>> channels = result.getChannels();
for (PNFetchMessageItem messageItem : channels.get("my_channel")) {
System.out.println(messageItem.getMessage());
System.out.println(messageItem.getMeta());
System.out.println(messageItem.getTimetoken());
HashMap<String, HashMap<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());
}
}
}
}
} else {
status.getErrorData().getThrowable().printStackTrace();
}
}
});

Go to SDK

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());
}
}
}
});

For more details on working with message history, refer to Fetching Messages from Storage.

←Sending MessagesUnread Counts→
  • Fetch messages from channels
    • Start and end parameters
© PubNub Inc. - Privacy Policy