Message Persistence API for PubNub Python SDK
Message Persistence provides real-time access to the history of 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 locations. Stored messages can be encrypted with AES-256 message encryption, ensuring that they are not readable while stored on PubNub's network. For more information, refer to Message Persistence.
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 day, 7 days, 30 days, 3 months, 6 months, 1 year, or Unlimited.
You can retrieve the following:
- Messages
- Message reactions
- File Sharing (using File Sharing API)
Request execution and return values
You can decide whether to perform the Python SDK operations synchronously or asynchronously.
-
.sync()
returns anEnvelope
object, which has two fields:Envelope.result
, whose type differs for each API, andEnvelope.status
of typePnStatus
.pubnub.publish() \
.channel("myChannel") \
.message("Hello from PubNub Python SDK") \
.sync() -
.pn_async(callback)
returnsNone
and passes the values ofEnvelope.result
andEnvelope.status
to a callback you must define beforehand.def my_callback_function(result, status):
print(f'TT: {result.timetoken}, status: {status.category.name}')
pubnub.publish() \
.channel("myChannel") \
.message("Hello from PubNub Python SDK") \
.pn_async(my_callback_function)
Fetch History
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
This function fetches historical messages from one or multiple channels. The includeMessageActions
flag also allows you to fetch message reactions along with the messages.
It's possible to control how messages are returned and in what order.
- if you specify only the
start
parameter (withoutend
), you will receive messages that are older than thestart
timetoken - if you specify only the
end
parameter (withoutstart
), you will receive messages from thatend
timetoken and newer - if you specify values for both
start
andend
parameters, you will retrieve messages between those timetokens (inclusive of theend
value)
You will receive a maximum of 100 messages for a single channel or 25 messages for multiple channels (up to 500). If more messages meet the timetoken criteria, make iterative calls while adjusting the start
timetoken to fetch the entire list of messages from Message Persistence.
Method(s)
To run Fetch History
, you can use the following method(s) in the Python SDK:
pubnub.fetch_messages() \
.channels(List) \
.maximum_per_channel(Integer) \
.start(Integer) \
.end(Integer) \
.include_message_actions(Boolean) \
.include_meta(Boolean)
.include_message_type(Boolean) \
.include_custom_message_type(Boolean) \
.include_uuid(Boolean) \
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channels | List<string> | Yes | n/a | Specifies the channels for which to return history. Maximum of 500 channels are allowed. |
maximum_per_channel | Integer | No | 25 or 100 | Specifies the number of historical messages to return. If include_message_actions is True , method is limited to single channel and 25 is the default (and maximum) value; otherwise, default and maximum is 100 for a single channel, or 25 for multiple channels. |
start | Integer | No | n/a | Timetoken delimiting the exclusive start of the time slice from which to pull messages. |
end | Integer | No | n/a | Timetoken delimiting the inclusive end of the time slice from which to pull messages. |
include_message_actions | Boolean | No | False | Set to True to retrieve history messages with their associated message reactions. If you include message reactions, the fetch_messages() method is limited to one channel only. |
include_meta | Boolean | No | False | Whether to include message metadata within response or not. |
include_message_type | Boolean | No | n/a | Indicates whether to retrieve messages with PubNub message type. For more information, refer to Retrieving Messages. |
include_custom_message_type | Boolean | No | n/a | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
include_uuid | Boolean | No | n/a | Whether to include UUID of the sender |
Basic Usage
Retrieve the last message on a channel:
- Builder Pattern
- Named Arguments
def my_fetch_messages_callback(envelope, status):
if status.is_error():
print(f"Something went wrong. Error: {status.error_data}")
return
print("Fetch Messages Result:\n")
for message in envelope.channels["my_channel"]:
print("Message: %s" % message.message)
print("Meta: %s" % message.meta)
print("Timetoken: %s" % message.timetoken)
for action_type in message.actions:
print("\nMessage Reaction type: " + action_type)
for action_value in message.actions[action_type]:
print("Message Reaction value: %s" % action_value)
show all 29 linesmessage_envelope = pubnub.fetch_messages(channels=["my_channel"], maximum_per_channel=1, include_message_actions=True,
include_meta=True, include_message_type=True, include_custom_message_type=True, include_uuid=True).sync()
if message_envelope.status.is_error():
print(f"Something went wrong. Error: {status.error_data}")
else:
print("Fetch Messages Result:\n")
for message in message_envelope.result.channels["my_channel"]:
print(f"Message: {message.message}")
print(f"Meta: {message.meta}")
print(f"Timetoken: {message.timetoken}")
for action_type in message.actions:
print(f"Message Reaction type: {action_type}")
for action_value in message.actions[action_type]:
show all 20 linesReturns
The fetch_messages()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNFetchMessagesResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNFetchMessagesResult
Method | Type | Description |
---|---|---|
channels | Dictionary | Dictionary of PNFetchMessageItem |
start_timetoken | Int | Start timetoken |
end_timetoken | Int | End timetoken |
PNFetchMessageItem
Method | Type | Description |
---|---|---|
message | String | The message |
meta | Any | Meta value |
message_type | Any | Type of the message |
custom_message_type | Any | Custom type of the message |
uuid | String | UUID of the sender |
timetoken | Int | Timetoken of the message |
actions | List | A 3-dimensional List of message reactions, grouped by action type and value |
Delete Messages from History
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Removes the messages from the history of a specific channel.
Required setting
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 SDK.
pubnub.delete_messages() \
.channel(String) \
.start(Integer) \
.end(Integer) \
.sync()
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channel | String | Yes | Specifies channels to delete messages from. | |
start | Integer | Optional | Timetoken delimiting the start of time slice (inclusive) to delete messages from. | |
end | Integer | Optional | Timetoken delimiting the end of time slice (exclusive) to delete messages from. |
Basic Usage
- Builder Pattern
- Named Arguments
envelope = PubNub(pnconf).delete_messages() \
.channel("my-ch") \
.start(123) \
.end(456) \
.sync()
envelope = pubnub.delete_messages(channels=["my_channel"], start=123, end=456).sync()
Returns
The delete_messages()
operation doesn't have a return value.
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 example, if 15526611838554310
is the publish timetoken
, pass 15526611838554309
in Start
and 15526611838554310
in End
parameters respectively as shown in the following code snippet.
- Builder Pattern
- Named Arguments
envelope = PubNub(pnconf).delete_messages() \
.channel("my-ch") \
.start(15526611838554309) \
.end(15526611838554310) \
.sync()
envelope = pubnub.delete_messages(channels="my-ch", start=15526611838554309, end=15526611838554310).sync()
Message Counts
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Returns the number of messages published on one or more channels since a given time. The count
returned is the number of messages in history with a timetoken
value greater than or equal to the passed value in the channel_timetokens
parameter.
Unlimited message retention
For keys with unlimited message retention enabled, this method considers only messages published in the last 30 days.
Method(s)
You can use the following method(s) in the Python SDK:
pn.message_counts() \
.channel(String) \
.channel_timetokens(List)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
channel | String | Yes | The channels to fetch the message count. Single channel or multiple channels, separated by comma are accepted. | |
channel_timetokens | List | Yes | null | A list of timetokens ordered the same way as channels. Timetokens can be str or int type. |
Basic Usage
- Builder Pattern
- Named Arguments
envelope = pn.message_counts() \
.channel('unique_1') \
.channel_timetokens([15510391957007182]) \
.sync() \
print(envelope.result.channels['unique_1'])
envelope = pubnub.message_counts(channels="my-ch", channel_timetokens=[15510391957007182]).sync()
Returns
The message_counts()
operation returns an Envelope
which contains the following fields:
Field | Type | Description |
---|---|---|
result | PNMessageCountResult | A detailed object containing the result of the operation. |
status | PNStatus | A status object with additional information. |
PNMessageCountResult
Field | Type | Description |
---|---|---|
channels | Dictionary | A dictionary with the number of missed messages for each channel. |
Other Examples
Retrieve count of messages using different timetokens for each channel
- Builder Pattern
- Named Arguments
envelope = pn.message_counts() \
.channel('unique_1,unique_100') \
.channel_timetokens([15510391957007182, 15510391957007184]) \
.sync()
print(envelope.result.channels)
envelope = pubnub.message_counts(channels="unique_1,unique_100",
channel_timetokens=[15510391957007182, 15510391957007184]).sync()
History (deprecated)
Requires Message Persistence
This method requires that Message Persistence is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.
Alternative method
This method is deprecated. Use fetch history instead.
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 SDK:
pubnub.history() \
.channel(String) \
.include_meta(True) \
.reverse(Boolean) \
.include_timetoken(Boolean) \
.start(Integer) \
.end(Integer) \
.count(Integer)
Parameter | Type | Required | Default | 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 | Integer | Optional | Timetoken delimiting the start of time slice (exclusive) to pull messages from. | |
end | Integer | Optional | Timetoken delimiting the end of time slice (inclusive) to pull messages from. | |
count | Integer | Optional | Specifies the number of historical messages to return. |
tip
reverse
parameterMessages 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 = pubnub.history() \
.channel("history_channel") \
.count(100) \
.sync()
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 | Integer | Start timetoken . |
end_timetoken | Integer | End timetoken . |
PNHistoryItemResult
Method | Type | Description |
---|---|---|
timetoken | Integer | 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 = pubnub.history() \
.channel("my_channel") \
.count(3) \
.reverse(True) \
.sync()
Response
{
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
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)
pubnub.history()\
.channel("my_channel")\
.start(13847168620721752)\
.reverse(True)\
.sync()
Response
{
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
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)
pubnub.history()\
.channel("my_channel")\
.count(100)\
.start(-1)\
.end(13847168819178600)\
.reverse(True)\
.sync()
Response
{
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
show all 17 linesHistory Paging Example
Usage
You can call the method by passing 0 or a valid timetoken as the argument.
def get_all_messages(start_tt):
def history_callback(result, status):
msgs = result.messages
start = result.start_timetoken
end = result.end_timetoken
count = len(msgs)
if count > 0:
print("%d" % count)
print("start %d" % start)
print("end %d" % end)
if count == 100:
get_all_messages(start)
show all 22 linesInclude timetoken in history response
pubnub.history()\
.channel("my_channel")\
.count(100)\
.include_timetoken()
.sync()