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.

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

Fetch 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.

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

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

Last updated on