Message Persistence API: history() method that is not recommended for Python SDK

Not recommendedMessage Persistence is a current API. The history() method is an older way to read it. New apps should use fetch_messages(). Go to Message Persistence

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().

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 reverse set to False.
  • To search from the oldest end of the timeline, set reverse to True.
  • To page through results, provide start or end.
  • To retrieve a slice of the timeline, provide both start and end.
  • 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
required
channel required#
Type: str
Default: no default
ID of the channel to return history messages from. An empty value fails validation with the message Channel missing.
reverse#
Type: bool
Default: False
Whether to traverse the timeline from the oldest message to the newest.
include_timetoken#
Type: bool
Default: False
Whether each message in the response carries its timetoken. Pass the value, as in include_timetoken(True).
include_meta#
Type: bool
Default: False
Whether each message in the response carries the metadata that its publisher set at publish time.
start#
Type: int
Default: no default
Timetoken that sets the newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. With end, start must be a higher timetoken than end. The service rejects -1 with an HTTP 400 error.
end#
Type: int
Default: no default
Timetoken that sets the older boundary of the query (inclusive). Retrieval stops at this timetoken. With start, end must be a lower timetoken than start. With reverse set to True, end becomes the newer boundary, so the response holds the older messages.
count#
Type: int
Default: 100
Number of messages to return. The maximum is 100, and history() replaces a larger value or a value below 1 with 100.

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.

messages#
Type: list
The returned messages, oldest first. Each item is a PNHistoryItemResult.
start_timetoken#
Type: int
Timetoken of the oldest returned message.
end_timetoken#
Type: int
Timetoken of the newest returned message.

Each PNHistoryItemResult has these properties:

entry#
Type: any
The message payload.
timetoken#
Type: int
Timetoken of the message. None without include_timetoken.
meta#
Type: any
Metadata that the publisher set. None without include_meta or when the message has none.
error#
Type: Exception
Decryption error, or None.

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 lines

Include the timetoken in the response​

1envelope = pubnub.history() \
2 .channel("history_channel") \
3 .count(100) \
4 .include_timetoken(True) \
5 .sync()

Was this page useful?

Last updated on