Message Persistence API for Python-Asyncio SDK

The PubNub Python-Asyncio SDK reads, deletes, and counts the messages that Message Persistence stores, with fetch_messages(), delete_messages(), and message_counts(). Message Persistence must be enabled on the keyset before you store or read history, and new keysets don't enable it by default.

Each builder method has a keyword argument of the same name, except where a table says otherwise.

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.

Message Persistence gives you real-time access to the history of messages published to PubNub. Each message is timestamped to the nearest 10 nanoseconds and stored across multiple availability zones in several geographic locations. You can encrypt stored messages with AES-256 so they are not readable on PubNub’s network. For details, see Message Persistence.

You control how long messages are stored through your account’s retention policy. Options include: 1 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.

You can retrieve the following:

  • Messages
  • Message reactions
  • Files (using the File Sharing API)

Fetch history​

fetch_messages() returns the messages that Message Persistence stores for one or more channels, using a PubNub client for Python-Asyncio.

The start and end timetokens control which messages the call returns and in what order.

  • If you specify only start, the call returns messages older than that timetoken, because start is the newer, exclusive boundary of the query.
  • If you specify only end, the call returns messages with a timetoken equal to or newer than end, because end is the older, inclusive boundary of the query.
How to use the start and end parameters

PubNub retrieves messages by searching backward through time (newest to oldest). Because of this, the parameter names are the reverse of their intuitive meaning:

  • start is the newer boundary (higher timetoken value) - search begins here, exclusive
  • end is the older boundary (lower timetoken value) - search stops here, inclusive

When you provide both parameters, start must be a higher timetoken than end.

9:00 AM10:00 AM11:00 AM11:59 AM

Results are always returned in oldest-first order.

A single history call returns up to 100 messages for one channel, or up to 25 messages per channel across as many as 500 channels.If more messages match the time range, call again and set start to the timetoken of the oldest message in the previous response.The start and end parameters take an int, so convert a timetoken string with int().
required
channels required#
Type: str or list of str
Default: no default
IDs of the channels to fetch messages from, up to 500 channels. Pass a list of channel names or one string. A string is split on commas, so "ch1,ch2" names two channels. With more than one channel and include_message_actions set to True fails validation with the message History can return message action data for a single channel only. Either pass a single channel or disable the include_message_actions flag.
count#
Type: int
Default: no default
Number of messages to return for each channel. The default is 100 for one channel, and 25 for several channels or with include_message_actions set to true. The SDK lowers a larger value to that default. The builder also accepts maximum_per_channel() as an alias.
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.
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.
include_meta#
Type: bool
Default: False
Whether each returned message includes the metadata that its publisher set at publish time, in the meta property.
include_message_actions#
Type: bool
Default: False
Whether the response includes the message actions of each message, which limits the call to one channel and 25 messages.
include_message_type#
Type: bool
Default: no default
Whether each returned message includes its PubNub message type in the message_type property.
include_custom_message_type#
Type: bool
Default: no default
Whether the request asks PubNub to include the custom message type of each message. The SDK sends the flag, but the items of the result do not expose a custom message type. Only the builder form sets it. For the background, see Message Persistence.
include_uuid#
Type: bool
Default: no default
Whether each returned message includes the User ID of its publisher in the uuid property.
decrypt#
Type: bool
Default: False
Whether the SDK decrypts each message with the crypto module of the client. Without it, message holds the stored payload. If decryption fails, the item keeps the stored payload and sets error. The keyword argument of fetch_messages() is decrypt_messages, and the builder method is decrypt_messages().

Returns​

fetch_messages() resolves with an AsyncioEnvelope whose result is a PNFetchMessagesResult and whose status is a PNStatus.

PNFetchMessagesResult​

channels#
Type: dict
Maps each channel name to a list of PNFetchMessageItem objects, oldest first.
start_timetoken#
Type: int
The start value of the request, or None when the request set none.
end_timetoken#
Type: int
The end value of the request, or None when the request set none.

PNFetchMessageItem​

message#
Type: any
The message payload.
timetoken#
Type: str
Timetoken that PubNub assigned to the message.
meta#
Type: any
Metadata that the publisher set at publish time, or None when the message has none.
uuid#
Type: str
User ID of the publisher. Present only when the response holds it, for example with include_uuid.
message_type#
Type: any
PubNub message type. Present only when the response holds it, for example with include_message_type.
actions#
Type: dict
Message actions on the message, grouped by action type and then by action value. Each action is a dict with uuid and actionTimetoken. An empty dict when the message has no actions. None without include_message_actions.
error#
Type: Exception
Decryption error, or None. Set only when the SDK fails to decrypt the message, and message then holds the stored payload.

With include_message_actions set to true, PubNub truncates the response when the returned messages hold more than 25000 message actions in total. The Python result has no more property, so it does not report where the truncation stopped. Lower count to fetch fewer messages in each call.

Syntax

1await pubnub.fetch_messages() \
2 .channels(channels: Union[str, List[str]]) \
3 .count(count: int) \
4 .start(start: int) \
5 .end(end: int) \
6 .include_meta(include_meta: bool) \
7 .include_message_actions(include_message_actions: bool) \
8 .include_message_type(include_message_type: bool) \
9 .include_custom_message_type(include_custom_message_type: bool) \
10 .include_uuid(include_uuid: bool) \
11 .decrypt_messages(decrypt: bool) \
12 .future() -> AsyncioEnvelope

Delete messages from history​

delete_messages() deletes stored messages from channel history, optionally within a time range, using a PubNub client for Python-Asyncio.

Delete-From-History must be enabled for the keyset in the Admin Portal before a delete call succeeds. A call on a keyset where it is off fails with a 403 error.

The start timetoken is always the exclusive boundary and the end timetoken is always the inclusive boundary. The call deletes the message at end but not the message at start, whichever of the two values is larger. For the full walkthrough, see Delete messages from history.

required
channel required#
Type: str
Default: no default
ID of the channel to delete messages from. An empty value fails validation with the message Channel missing.
start#
Type: int
Default: no default
Timetoken that sets the exclusive boundary of the range to delete. The call keeps the message at this timetoken.
end#
Type: int
Default: no default
Timetoken that sets the inclusive boundary of the range to delete. The call deletes the message at this timetoken.

Returns​

delete_messages() resolves with an AsyncioEnvelope.

result#
Type: dict
An empty dictionary.
status#
Type: PNStatus
The operation status.

To delete one message, pass its publish timetoken, which a successful publish() returns, as end. Pass the next higher or the next lower timetoken as start. For example, if 15526611838554310 is the publish timetoken, pass 15526611838554310 as end and either 15526611838554309 or 15526611838554311 as start. Both pairs delete only the message at 15526611838554310.

Syntax

1await pubnub.delete_messages() \
2 .channel(channel: str) \
3 .start(start: int) \
4 .end(end: int) \
5 .future() -> AsyncioEnvelope

Message counts​

message_counts() returns the number of messages published to one or more channels since a timetoken, using a PubNub client for Python-Asyncio.

The count includes every message with a timetoken greater than the timetoken you pass for the channel, so a message at exactly that timetoken is not counted.On a keyset with Unlimited retention, message_counts() considers only messages published in the last 30 days.
required
channel required#
Type: str or list of str
Default: no default
IDs of the channels to count messages in. Pass a list of channel names or one string. A string is split on commas, so "ch1,ch2" names two channels. The keyword argument of message_counts() is channels. An empty value fails validation with the message Channel missing.
channel_timetokens required#
Type: list of int or str
Default: no default
Timetokens from which to count messages, listed in the same order as channel. The list must hold exactly one timetoken per channel. Each item is an int or a str. The keyword argument of message_counts() is channels_timetoken. A list whose length differs from the number of channels fails validation with the message The number of channels and the number of timetokens do not match..

Returns​

message_counts() resolves with an AsyncioEnvelope whose result is a PNMessageCountResult and whose status is a PNStatus.

PNMessageCountResult​

channels#
Type: dict
Maps each channel name to its message count. A channel without messages has a count of 0. A channel with 10,000 messages or more has a count of 10000.

Syntax

1await pubnub.message_counts() \
2 .channel(channel: Union[str, List[str]]) \
3 .channel_timetokens(channel_timetokens: List[Union[int, str]]) \
4 .future() -> AsyncioEnvelope

history() is a method of the PubNub Python-Asyncio SDK that fetches the messages of one channel. Its use is not recommended, and fetch_messages() replaces it.

The reference for apps that still call history() is Message Persistence API: history() method that is not recommended for Python-Asyncio SDK. New apps use Fetch history.

Was this page useful?

Last updated on