Message history
PubNub allows you to store and retrieve messages as they get sent over the network by using the Message Persistence 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.
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.
Fetch messages from channels
Use the fetchMessages
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).
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
- Swift
- Java
- Unity
// 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(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);
show all 28 linespubnub.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());
show all 19 linesFetch messages with reactions
Use the fetchMessages
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.
Count limits
While fetching messages with reactions, the count
parameter has a max value of 25.
- JavaScript
- Swift
- Java
- Unity
// start, end, count are optional
const response = await pubnub.fetchMessages({
channels: ['ch-1'],
includeMessageActions: true,
end: '15343325004275466',
count: 25
});
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)")
}
}
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()) {
show all 29 linespubnub.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());
show all 20 linesFor more details on working with message history, refer to Fetching Messages from Message Persistence.