Retrieve old messages

Even though real-time in-app messaging has the obvious benefit of receiving data nearly instantly, it's impossible to keep track of all incoming messages. There are times you might be more interested in messages that have been sent in the past.

PubNub allows you to retain and retrieve historical messages. All you need to know is the channel name the message was sent to and the message's timetoken. Beforehand, however, you must enable Message Persistence in the Admin Portal and configure the amount of time your messages are stored for.

You can retrieve historical messages from one or multiple channels. You can also control the time range and influence the return order. A single request may retrieve up to 100 messages for a single channel or 25 messages for multiple channels (up to 500). If you're not interested in the message content, you may also retrieve the number of messages you missed.

Most use cases require retrieving messages missed since a device or user was last online. To do that, you only provide the end parameter with a 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 the messages have any emojis, reactions, or delivery acknowledgments attached, you may retrieve them as well.

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