Chat 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.
The method returns up to 100 messages on a single channel and 25 messages per channel on a maximum of up to 500 channels.
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).
// start, end, count are optional
pubnub.fetchMessages(
{
channels: ['ch-1'],
end: '15343325004275466',
count: 100
},
(status, response) => {
// handle response
}
);
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)")
}
}
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();
}
}
});
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.