How do I retrieve messages from history going from most recent to oldest?
For the use case of paging back through history from the most recent message to the oldest message:
Make a request specifying no start point.
- The result will be in the format of: '[[message1, message2, ... messageN], oldesttimetoken, newesttimetoken]'
- The order of the messages returned is always oldest to newest
- The oldest timetoken is for the left-most message
- The newest timetoken is for the right most message
- Because we are paging back through history, newest to oldest, in our next request we want to start with the oldest returned message from our previous request
- Make another request that specifies
end=<newest timetoken from previous request>. Because the end point of a request is always exclusive (not included in the result), we can page over the results without any overlap
- Continue to make requests recursively specifying the end parameter using the newest token from the previous request until the list of messages returned is less than the count requested (max default = 100). For example, if the result set is < 100 when we have requested 100, we can safely assume this is the last page of results.
For example, the first two requests may look like the below:
Get 5 most recent messages
- http://pubsub.pubnub.com/v2/history/sub-key/ /channel/ ?count=5
- Result (actual messages removed for brevity): [[msg, msg, ...],13709566381183822, 13709577810091267]
Get next 5 messages preceding the oldest returned in the result above
http://pubsub.pubnub.com/v2/history/sub-key/ /channel/ ?count=5&start=13709566381183822
An example use case may specify: "I want to page back through history from most recent to oldest message", but with the added parameter of "But only go back through 5 hours of history". In this case, all history requests (both the initial request, and each paged request afterwards) would also include an end point of 5 hours in the past. This would restrict the results to only messages published within the last 5 hours. Once paging to a point where there were no messages within the bounds of the specified end point, it would return an empty set.
- Get 5 most recent messages (with an end point 5 hours in past)
- http://pubsub.pubnub.com/v2/history/sub-key/ /channel/ ?count=5&end=
- Result (actual messages removed for brevity): [[msg, msg, ...],13709566381183822,13709577810091267]
Get next 5 older messages (bounded by an endpoint 5 hours in the past).
http://pubsub.pubnub.com/v2/history/sub-key/ /channel/ ?count=5&start=13709566381183822&end=
Once our result set is empty (or smaller than the requested count) we can safely assume there are no more messages within the given timespan.