PubNub allows you to store and retrieve messages as they get sent over the network by using the Storage & 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, 7 days, 1 month, and Forever.
Retrieving unread counts of messages
The messageCounts method returns a count of unread messages in one or more channels. The count returned is the number of messages in history with a timetoken value greater than the passed value in the timetoken parameter.
To use this feature, you need to cache the timetoken of either the last retrieved message on a channel, or the last time the user was online.
Fetching missed messages
Use this method to catch up on missed messages when a device comes back online. The request returns a list of messages that were published before the start timetoken and after the end timetoken.
The method returns a maximum of 25 messages per channel. To retrieve more messages, use timestamps to page through the next batch of messages.
Start and End parameters
Messages are always returned in 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).
Retrieving most recent messages
Use the fetchMessages() method to fetch the most recent messages that were published in the specified channel(s) before the current time.
The method returns a maximum of 25 messages per channel. To retrieve more messages, use the timestamps to page through the next batch of messages.