Message Persistence API for Python-Asyncio SDK
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)
History
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
This function fetches historical messages of a channel.
You can control how messages are returned and in what order:
- Search from the newest end of the timeline (default:
reverse=False). - Search from the oldest end of the timeline by setting
reversetoTrue. - Page through results by providing a
startorendtimetoken. - Retrieve a slice of the timeline by providing both a
startandendtimetoken. - Limit the number of messages using the
countparameter.
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:
startis the newer boundary (higher timetoken value) - search begins here, exclusiveendis the older boundary (lower timetoken value) - search stops here, inclusive
When you provide both parameters, start must be a higher timetoken than end.
Results are always returned in oldest-first order.
Method(s)
To run History you can use the following method(s) in the Python-asyncio SDK:
1pubnub.history() \
2 .channel(String) \
3 .include_meta(True) \
4 .reverse(Boolean) \
5 .include_timetoken(Boolean) \
6 .start(Int) \
7 .end(Int) \
8 .count(Int)
| Parameter | Description |
|---|---|
channel *Type: String | Specifies channel to return history messages from. |
include_metaType: Boolean | Specifies whether or not the message's meta information should be returned. |
reverseType: Boolean | Whether to traverse the timeline in reverse starting with the oldest message first. |
include_timetokenType: Boolean | Whether event dates timetokens should be included in the response. |
startType: Int | Newer boundary of the query (exclusive). Retrieval begins at this timetoken and moves backward in time. Must be a higher timetoken value than end when both are provided. |
endType: Int | Older boundary of the query (inclusive). Retrieval stops at this timetoken. Must be a lower timetoken value than start when both are provided. |
countType: Int | 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.
Sample code
Reference code
Retrieve the last 100 messages on a channel:
1import asyncio
2import os
3from pubnub.pnconfiguration import PNConfiguration
4from pubnub.pubnub_asyncio import PubNubAsyncio
5from pubnub.exceptions import PubNubException
6
7
8async def get_history(pubnub: PubNubAsyncio):
9 try:
10 # Retrieve the last 100 messages on a channel
11 envelope = await pubnub.history() \
12 .channel('history_channel') \
13 .count(100) \
14 .future()
15
show all 42 linesReturns
The history() operation returns a PNHistoryResult which contains the following properties:
| Method | Description |
|---|---|
messagesType: List | List of messages of type PNHistoryItemResult. See PNHistoryItemResult for more details. |
start_timetokenType: Int | Start timetoken. |
end_timetokenType: Int | End timetoken. |
PNHistoryItemResult
| Method | Description |
|---|---|
timetokenType: Int | Timetoken of the message. |
entryType: Object | Message. |
Other examples
Use history() to retrieve the three oldest messages by retrieving from the time line in reverse
1envelope = await pubnub.history() \
2 .channel("my_channel") \
3 .count(3) \
4 .reverse(True) \
5 .future()
Response
1{
2 end_timetoken: 13406746729185766,
3 start_timetoken: 13406746780720711,
4 messages: [{
5 crypto: None,
6 entry: 'Pub1',
7 timetoken: None
8 },{
9 crypto: None,
10 entry: 'Pub2',
11 timetoken: None
12 },{
13 crypto: None,
14 entry: 'Pub2',
15 timetoken: None
show all 17 linesUse 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)
1envelope = await pubnub.history()\
2 .channel("my_channel")\
3 .start(13847168620721752)\
4 .reverse(true)\
5 .future()
Response
1{
2 end_timetoken: 13406746729185766,
3 start_timetoken: 13406746780720711,
4 messages: [{
5 crypto: None,
6 entry: 'Pub4',
7 timetoken: None
8 },{
9 crypto: None,
10 entry: 'Pub5',
11 timetoken: None
12 },{
13 crypto: None,
14 entry: 'Pub6',
15 timetoken: None
show all 17 linesUse history() to retrieve messages until a given timetoken by paging from newest message to oldest message until a specific end point in time (inclusive)
1envelope = await pubnub.history()\
2 .channel("my_channel")\
3 .count(100)\
4 .start(-1)\
5 .end(13847168819178600)\
6 .reverse(True)\
7 .future()
Response
1{
2 end_timetoken: 13406746729185766,
3 start_timetoken: 13406746780720711,
4 messages: [{
5 crypto: None,
6 entry: 'Pub4',
7 timetoken: None
8 },{
9 crypto: None,
10 entry: 'Pub5',
11 timetoken: None
12 },{
13 crypto: None,
14 entry: 'Pub6',
15 timetoken: None
show all 17 linesHistory paging example
Usage
You can call the method by passing 0 or a valid timetoken as the argument.
1async def get_all_messages(start_tt):
2 envelope = await pubnub.history()\
3 .channel('history_channel')\
4 .count(100)\
5 .start(start_tt)\
6 .future()
7
8 msgs = envelope.result.messages
9 start = envelope.result.start_timetoken
10 end = envelope.result.end_timetoken
11 count = len(msgs)
12
13 if count > 0:
14 print("%d" % count)
15 print("start %d" % start)
show all 22 linesInclude timetoken in history response
1envelope = await pubnub.history()\
2 .channel("my_channel")\
3 .count(100)\
4 .include_tometoken()
5 .future()
Delete messages from history
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Remove messages from the history of a specific channel.
Required setting
To accept delete-from-history requests, enable the Delete-From-History setting for your key in the Admin Portal and initialize the SDK with a secret key.
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:
startis the newer boundary (higher timetoken value) - search begins here, exclusiveendis the older boundary (lower timetoken value) - search stops here, inclusive
When you provide both parameters, start must be a higher timetoken than end.
Results are always returned in oldest-first order.
Method(s)
To Delete Messages from History you can use the following method(s) in the Python-asyncio SDK.
1pubnub.delete_messages() \
2 .channel(String) \
3 .start(Int) \
4 .end(Int) \
5 .future()
| Parameter | Description |
|---|---|
channel *Type: String | Specifies channels to delete messages from. |
startType: Int | Timetoken delimiting the start of time slice (inclusive) to delete messages from. |
endType: Int | Timetoken delimiting the end of time slice (exclusive) to delete messages from. |
Sample code
1envelope = yield from pubnub.delete_messages()\
2 .channel("my-ch")\
3 .start(123)\
4 .end(456)
5 .future()
Other examples
Delete specific message from history
To delete a specific message, pass the publish timetoken (received from a successful publish) in the End parameter and timetoken +/- 1 in the Start parameter. For exmaple, if 15526611838554310 is the publish timetoken, pass 15526611838554309 in Start and 15526611838554310 in End parameters respectively as shown in the following code snippet.
1envelope = yield from pubnub.delete_messages()\
2 .channel("my-ch")\
3 .start(15526611838554309)\
4 .end(15526611838554310)
5 .future()