Message Persistence API: history() method that is not recommended for Python-Asyncio 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-Asyncio SDK. The 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.

A builder sends its request when you await its future(). A request that fails, or fails validation, does not raise an exception. future() resolves with a PubNubAsyncioException whose result is None and whose status holds the error. Call is_error() on the awaited value before you read result.

1await pubnub.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 .future() -> AsyncioEnvelope
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 = await pubnub.history() \
2 .channel("history_channel_2") \
3 .count(100) \
4 .future()
5if envelope.is_error():
6 print(f"Error: {envelope.status.error_data}")

Returns​

history() resolves with an AsyncioEnvelope 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 = await pubnub.history() \
2 .channel("history_channel_2") \
3 .count(3) \
4 .reverse(True) \
5 .future()
6if envelope.is_error():
7 print(f"Error: {envelope.status.error_data}")

Retrieve messages newer than a timetoken​

Page from the oldest message to the newest, starting at a single point in time (exclusive).

1envelope = await pubnub.history() \
2 .channel("history_channel_2") \
3 .start(tokens[1]) \
4 .reverse(True) \
5 .future()
6if envelope.is_error():
7 print(f"Error: {envelope.status.error_data}")

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 = await pubnub.history() \
2 .channel("history_channel_2") \
3 .count(100) \
4 .end(tokens[2]) \
5 .future()
6if envelope.is_error():
7 print(f"Error: {envelope.status.error_data}")

Page through all messages​

Call the function with a valid timetoken, for example one above the newest message.

1async def get_all_messages(start_tt):
2 envelope = await pubnub.history() \
3 .channel("history_paging_channel_2") \
4 .count(100) \
5 .start(start_tt) \
6 .future()
7 if envelope.is_error():
8 print(f"Error: {envelope.status.error_data}")
9 return
10 msgs = envelope.result.messages
11 start = envelope.result.start_timetoken
12 end = envelope.result.end_timetoken
13 count = len(msgs)
14
15 if count > 0:
show all 21 lines

Include the timetoken in the response​

1envelope = await pubnub.history() \
2 .channel("history_channel_2") \
3 .count(100) \
4 .include_timetoken(True) \
5 .future()
6if envelope.is_error():
7 print(f"Error: {envelope.status.error_data}")

Was this page useful?

Last updated on