Retrieve old messages

Real-time in‑app messaging delivers data quickly. Sometimes you need messages sent earlier.

PubNub lets you retain and retrieve historical messages. You need the channel name and the message’s timetoken. A timetoken is a 17‑digit timestamp created when PubNub publishes a message. Enable Message Persistence in the Admin Portal. Then set how long PubNub stores messages. PubNub manages retention with storage periods based on your plan.

You can retrieve historical messages from one or multiple channels. You can also control the time range and choose the return order. A request returns up to 100 messages for one channel. For multiple channels, it returns up to 25 per channel, with a maximum of 500 total. If you only need counts, retrieve the number of messages you missed.

Most use cases retrieve messages missed since the client was last online. Provide the end parameter with the timetoken of the last received message. The following code returns the last 100 messages on the chats_guilds.mages_guild channel.

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.

pubnub.fetchMessages(
{
channels: ["chats_guilds.mages_guild"],
end: '15343325004275466',
count: 100
},
function(status, response) {
console.log(status, response);
}
);

If messages include any emojis, actions, or delivery acknowledgments, you can retrieve them. By default, history doesn’t include the custom message type. You can enable it.

Having read how to work with historical messages, let's focus back on the present. As your channels get more traction and users start to subscribe, it's worthwhile to understand how to check who's currently online.

Last updated on
On this page