Message Persistence API for PubNub Python-asyncio SDK
Python version support
Python SDK versions 5.0.0 and higher no longer support Python v2.7 and the Twisted and Tornado frameworks. If you require support for any of these, use SDK version 4.8.1.
Note that PubNub will stop supporting versions of Python lower than 3.7 by the end of 2021.
The PubNub Message Persistence Service provides real-time access to history for all messages published to PubNub. Each published message is timestamped to the nearest 10 nanoseconds, and is stored across multiple availability zones in several geographical data center locations. Stored messages can be encrypted with AES-256 message encryption, ensuring that they are not readable while stored on PubNub's network.
Messages can be stored for a configurable duration or forever, as controlled by the retention policy that is configured on your account. The following options are available: 1, 3, 5, 7, 15, or 30 days, and Forever.
History
Requires Message Persistence add-on Requires that the Message Persistence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
This function fetches historical messages of a channel.
It is possible to control how messages are returned and in what order, for example you can:
- Search for messages starting on the newest end of the timeline (default behavior -
reverse
=False
) - Search for messages from the oldest end of the timeline by setting
reverse
toTrue
. - Page through results by providing a
start
ORend
timetoken. - Retrieve a slice of the time line by providing both a
start
ANDend
timetoken. - Limit the number of messages to a specific quantity using the
count
parameter.
Start & End parameter usage clarity:
If only the start
parameter is specified (without end
), you will receive messages that are older than and up to that start
timetoken value. If only the end
parameter is specified (without start
) you will receive messages that match that end
timetoken value and newer Specifying values for both start
and end
parameters will return messages between those timetoken values (inclusive on the end
value). Keep in mind that you will still receive a maximum of 100 messages even if there are more messages that meet the timetoken values. Iterative calls to history adjusting the start
timetoken is necessary to page through the full set of results if more than 100 messages meet the timetoken values.
Method(s)
To run History
you can use the following method(s) in the Python-asyncio SDK:
pubnub.history().channel(String).include_meta(True).reverse(Boolean).include_timetoken(Boolean).start(Int).end(Int).count(Int)
Parameter Type Required Defaults Description channel
String Yes Specifies channel
to return history messages from.include_meta
Boolean No False
Specifies whether or not the message's meta information should be returned. reverse
Boolean Optional False
Setting to True
will traverse the time line in reverse starting with the oldest message first.include_timetoken
Boolean Optional False
Whether event dates timetokens should be included in response or not. start
Int Optional Timetoken delimiting the start of time slice (exclusive) to pull messages from. end
Int Optional Timetoken delimiting the end of time slice (inclusive) to pull messages from. count
Int Optional Specifies the number of historical messages to return.
Using the reverse
parameter:
Messages are always returned sorted in ascending time direction from history regardless of reverse
. The reverse
direction matters when you have more than 100 (or count
, if it's set) messages in the time interval, in which case reverse
determines the end of the time interval from which it should start retrieving the messages.
Basic Usage
Retrieve the last 100 messages on a channel:
envelope = await pubnub.history()\
.channel('history_channel')\
.count(100).future()
# handle messages stored at evelope.result.messages
# status is available as envelope.status
Returns
The history()
operation returns a PNHistoryResult
which contains the following fields:
Method | Type | Description |
---|---|---|
messages | List | List of messages of type PNHistoryItemResult . See PNHistoryItemResult for more details. |
start_timetoken | Int | Start timetoken . |
end_timetoken | Int | End timetoken . |
Method | Type | Description |
---|---|---|
timetoken | Int | Timetoken of the message. |
entry | Object | Message. |
Other Examples
Use history() to retrieve the three oldest messages by retrieving from the time line in reverse:
envelope = await pubnub.history().channel("my_channel").count(3).reverse(True).future()
{ end_timetoken: 13406746729185766, start_timetoken: 13406746780720711, messages: [{ crypto: None, entry: 'Pub1', timetoken: None },{ crypto: None, entry: 'Pub2', timetoken: None },{ crypto: None, entry: 'Pub2', timetoken: None }] }
Use history() to retrieve messages newer than a given timetoken by paging from oldest message to newest message starting at a single point in time (exclusive):
envelope = await pubnub.history()\ .channel("my_channel")\ .start(13847168620721752)\ .reverse(true)\ .future()
{ end_timetoken: 13406746729185766, start_timetoken: 13406746780720711, messages: [{ crypto: None, entry: 'Pub4', timetoken: None },{ crypto: None, entry: 'Pub5', timetoken: None },{ crypto: None, entry: 'Pub6', timetoken: None }] }
Use history() to retrieve messages until a given timetoken by paging from newest message to oldest message until a specific end point in time (inclusive):
envelope = await pubnub.history()\ .channel("my_channel")\ .count(100)\ .start(-1)\ .end(13847168819178600)\ .reverse(True)\ .future()
{ end_timetoken: 13406746729185766, start_timetoken: 13406746780720711, messages: [{ crypto: None, entry: 'Pub4', timetoken: None },{ crypto: None, entry: 'Pub5', timetoken: None },{ crypto: None, entry: 'Pub6', timetoken: None }] }
-
Usage!
You can call the method by passing 0 or a valid timetoken as the argument.
async def get_all_messages(start_tt): envelope = await pubnub.history()\ .channel('history_channel')\ .count(100)\ .start(start_tt)\ .future() msgs = envelope.result.messages start = envelope.result.start_timetoken end = envelope.result.end_timetoken count = len(msgs) if count > 0: print("%d" % count) print("start %d" % start) print("end %d" % end) if count == 100: await get_all_messages(start) loop = asyncio.get_event_loop() loop.run_until_complete(get_all_messages(14759343456292767))
Include timetoken in history response:
envelope = await pubnub.history()\ .channel("my_channel")\ .count(100)\ .include_tometoken() .future()
Delete Messages from History
Requires Message Persistence add-on Requires that the Message Persistence add-on is enabled for your key. See this page on enabling add-on features on your keys:
https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-
Description
Removes the messages from the history of a specific channel.
Note
There is a setting to accept delete from history requests for a key, which you must enable by checking the Enable Delete-From-History
checkbox in the key settings for your key in the Admin Portal.
Requires Initialization with secret key.
Method(s)
To Delete Messages from History
you can use the following method(s) in the Python-asyncio SDK.
pubnub.delete_messages().channel(String).start(Int).end(Int).future()
Parameter Type Required Defaults Description channel
String Yes Specifies channels
to delete messages from.start
Int Optional Timetoken delimiting the start
of time slice (inclusive) to delete messages from.end
Int Optional Timetoken delimiting the end
of time slice (exclusive) to delete messages from.
Basic Usage
envelope = yield from pubnub.delete_messages()\
.channel("my-ch")\
.start(123)\
.end(456)
.future()
Other Examples
Delete specific message from history
To delete a specific message, pass thepublish timetoken
(received from a successful publish) in theEnd
parameter andtimetoken +/- 1
in theStart
parameter. e.g. if15526611838554310
is thepublish timetoken
, pass15526611838554309
inStart
and15526611838554310
inEnd
parameters respectively as shown in the following code snippet.envelope = yield from pubnub.delete_messages()\ .channel("my-ch")\ .start(15526611838554310)\ .end(15526611838554309) .future()