Message Persistence API: history() method that is not recommended for Python SDK
This page documents the history() method of the PubNub Python SDK. The Python SDK source does not tag it as deprecated, but its use is not recommended. Message Persistence is a current API, and history() is an older way to read it. The method remains for apps that already use it. New apps call fetch_messages().
History (not recommended)
Not recommended
The use of history() is discouraged. Use fetch_messages() instead.
Requires Message Persistence add-on
This method requires that the Message Persistence add-on is enabled for your key in the Admin Portal. See Enable the features your app calls.
history() fetches the messages of one channel. These options control which messages it returns and in what order:
- By default, the search starts at the newest end of the timeline, which is
reverseset toFalse. - To search from the oldest end of the timeline, set
reversetoTrue. - To page through results, provide
startorend. - To retrieve a slice of the timeline, provide both
startandend. - To limit the number of messages, use
count.
Method(s)
history() takes no arguments, so it has no keyword form. Use the builder.
1pubnub.history() \
2 .channel(channel: str) \
3 .reverse(reverse: bool) \
4 .include_timetoken(include_timetoken: bool) \
5 .include_meta(include_meta: bool) \
6 .start(start: int) \
7 .end(end: int) \
8 .count(count: int) \
9 .sync() -> Envelope
channelrequired#- ID of the channel to return history messages from. An empty value fails validation with the message
Channel missing. reverse#- Whether to traverse the timeline from the oldest message to the newest.
include_timetoken#- Whether each message in the response carries its timetoken. Pass the value, as in
include_timetoken(True). include_meta#- Whether each message in the response carries the metadata that its publisher set at publish time.
start#- Timetoken that sets the newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. With
end,startmust be a higher timetoken thanend. The service rejects-1with an HTTP 400 error. end#- Timetoken that sets the older boundary of the query (inclusive). Retrieval stops at this timetoken. With
start,endmust be a lower timetoken thanstart. Withreverseset toTrue,endbecomes the newer boundary, so the response holds the older messages. count#- Number of messages to return. The maximum is
100, andhistory()replaces a larger value or a value below1with100.
history() always returns the messages sorted from the oldest to the newest, whatever reverse is. The reverse setting matters when more than 100 messages, or more than count if you set it, fall in the time interval. In that case, reverse decides which end of the interval the retrieval starts from.
Sample code
Retrieve the last 100 messages on a channel:
1envelope = pubnub.history() \
2 .channel("history_channel") \
3 .count(100) \
4 .sync()
Returns
history() returns an Envelope whose result is a PNHistoryResult and whose status is a PNStatus.
Each PNHistoryItemResult has these properties:
Other examples
Retrieve the three oldest messages
Set reverse to True to traverse the timeline from the oldest message first.
1envelope = pubnub.history() \
2 .channel("history_channel") \
3 .count(3) \
4 .reverse(True) \
5 .sync()
Retrieve messages newer than a timetoken
Page from the oldest message to the newest, starting at a single point in time (exclusive).
1envelope = pubnub.history() \
2 .channel("history_channel") \
3 .start(tokens[1]) \
4 .reverse(True) \
5 .sync()
Retrieve messages until a timetoken
Page from the newest message to the oldest, until a specific point in time (inclusive). Leave reverse unset, because reverse set to True makes end the newer boundary.
1envelope = pubnub.history() \
2 .channel("history_channel") \
3 .count(100) \
4 .end(tokens[2]) \
5 .sync()
Page through all messages
Call the function with a valid timetoken, for example one above the newest message.
1def get_all_messages(start_tt):
2 envelope = pubnub.history() \
3 .channel("history_paging_channel") \
4 .count(100) \
5 .start(start_tt) \
6 .sync()
7 msgs = envelope.result.messages
8 start = envelope.result.start_timetoken
9 end = envelope.result.end_timetoken
10 count = len(msgs)
11
12 if count > 0:
13 print("%d" % count)
14 print("start %d" % start)
15 print("end %d" % end)
show all 18 linesInclude the timetoken in the response
1envelope = pubnub.history() \
2 .channel("history_channel") \
3 .count(100) \
4 .include_timetoken(True) \
5 .sync()