Message Persistence API for Python 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)
Request execution and return values
You can decide whether to perform the Python SDK operations synchronously or asynchronously.
-
.sync()returns anEnvelopeobject, which has two fields:Envelope.result, whose type differs for each API, andEnvelope.statusof typePnStatus.1pubnub.publish() \
2 .channel("myChannel") \
3 .message("Hello from PubNub Python SDK") \
4 .sync() -
.pn_async(callback)returnsNoneand passes the values ofEnvelope.resultandEnvelope.statusto a callback you must define beforehand.1def my_callback_function(result, status):
2 print(f'TT: {result.timetoken}, status: {status.category.name}')
3
4pubnub.publish() \
5 .channel("myChannel") \
6 .message("Hello from PubNub Python SDK") \
7 .pn_async(my_callback_function)
Fetch history
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Fetch historical messages from one or more channels. Use include_message_actions to include message actions.
You can control how messages are returned and in what order.
- If you specify only the
startparameter (withoutend), you receive messages older than thestarttimetoken. - If you specify only the
endparameter (withoutstart), you receive messages from thatendtimetoken and newer. - If you specify both
startandend, you retrieve messages between those timetokens (inclusive of theendvalue).
You can receive up to 100 messages for a single channel. For multiple channels (up to 500), you can receive up to 25 messages per channel. If more messages match the time range, make iterative calls and adjust the start timetoken to page through the results.
Method(s)
Use the following method(s) in the Python SDK:
1pubnub.fetch_messages() \
2 .channels(List) \
3 .maximum_per_channel(Integer) \
4 .start(Integer) \
5 .end(Integer) \
6 .include_message_actions(Boolean) \
7 .include_meta(Boolean)
8 .include_message_type(Boolean) \
9 .include_custom_message_type(Boolean) \
10 .include_uuid(Boolean) \
| Parameter | Description |
|---|---|
channels *Type: List <string>Default: n/a | Specifies the channels for which to return history. Maximum of 500 channels are allowed. |
maximum_per_channelType: Integer Default: 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. |
startType: Integer Default: n/a | Timetoken delimiting the exclusive start of the time slice from which to pull messages. |
endType: Integer Default: n/a | Timetoken delimiting the inclusive end of the time slice from which to pull messages. |
include_message_actionsType: Boolean Default: False | Set to True to retrieve history messages with their associated message actions. If you include message actions, the fetch_messages() method is limited to one channel only. |
include_metaType: Boolean Default: False | Whether to include message metadata within response or not. |
include_message_typeType: Boolean Default: n/a | Indicates whether to retrieve messages with PubNub message type. For more information, refer to Retrieving Messages. |
include_custom_message_typeType: Boolean Default: n/a | Indicates whether to retrieve messages with the custom message type. For more information, refer to Retrieving Messages. |
include_uuidType: Boolean Default: n/a | Whether to include UUID of the sender |
Sample code
Reference code
Retrieve the last message on a channel:
- Builder Pattern
- Named Arguments
1import os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4
5
6def my_fetch_messages_callback(envelope, status):
7 if status.is_error():
8 print(f"Something went wrong. Error: {status.error_data}")
9 return
10
11 print("Fetch Messages Result:\n")
12 for message in envelope.channels["my_channel"]:
13 print("Message: %s" % message.message)
14 print("Meta: %s" % message.meta)
15 print("Timetoken: %s" % message.timetoken)
show all 48 lines1message_envelope = pubnub.fetch_messages(channels=["my_channel"], maximum_per_channel=1, include_message_actions=True,
2 include_meta=True, include_message_type=True, include_custom_message_type=True, include_uuid=True).sync()
3
4if message_envelope.status.is_error():
5 print(f"Something went wrong. Error: {status.error_data}")
6else:
7 print("Fetch Messages Result:\n")
8 for message in message_envelope.result.channels["my_channel"]:
9 print(f"Message: {message.message}")
10 print(f"Meta: {message.meta}")
11 print(f"Timetoken: {message.timetoken}")
12
13 for action_type in message.actions:
14 print(f"Message Action type: {action_type}")
15 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 | Description |
|---|---|
channelsType: Dictionary | Dictionary of PNFetchMessageItem |
start_timetokenType: Int | Start timetoken |
end_timetokenType: Int | End timetoken |
PNFetchMessageItem
| Method | Description |
|---|---|
messageType: String | The message |
metaType: Any | Meta value |
message_typeType: Any | Type of the message |
custom_message_typeType: Any | Custom type of the message |
uuidType: String | UUID of the sender |
timetokenType: Int | Timetoken of the message |
actionsType: List | A 3-dimensional List of message actions, grouped by action type and value |
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
Enable Delete-From-History for your key in the Admin Portal and initialize the SDK with a secret key.
Method(s)
To Delete Messages from History you can use the following method(s) in the Python SDK.
1pubnub.delete_messages() \
2 .channel(String) \
3 .start(Integer) \
4 .end(Integer) \
5 .sync()
| Parameter | Description |
|---|---|
channel *Type: String Default: n/a | Specifies channels to delete messages from. |
startType: Integer Default: n/a | Timetoken delimiting the start of time slice (inclusive) to delete messages from. |
endType: Integer Default: n/a | Timetoken delimiting the end of time slice (exclusive) to delete messages from. |
Sample code
- Builder Pattern
- Named Arguments
1envelope = PubNub(pnconf).delete_messages() \
2 .channel("my-ch") \
3 .start(123) \
4 .end(456) \
5 .sync()
1envelope = 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
1envelope = PubNub(pnconf).delete_messages() \
2 .channel("my-ch") \
3 .start(15526611838554309) \
4 .end(15526611838554310) \
5 .sync()
1envelope = pubnub.delete_messages(channels="my-ch", start=15526611838554309, end=15526611838554310).sync()
Message counts
Requires Message Persistence
Enable Message Persistence for your key in the Admin Portal. See how to enable add-on features.
Return the number of messages published since the given time. The count is the number of messages with a timetoken greater than or equal to the value in channel_timetokens.
Unlimited message retention
Only messages from the last 30 days are counted.
Method(s)
You can use the following method(s) in the Python SDK:
1pn.message_counts() \
2 .channel(String) \
3 .channel_timetokens(List)
| Parameter | Description |
|---|---|
channel *Type: String Default: n/a | The channels to fetch the message count. Single channel or multiple channels, separated by comma are accepted. |
channel_timetokens *Type: List Default: null | A list of timetokens ordered the same way as channels. Timetokens can be str or int type. |
Sample code
- Builder Pattern
- Named Arguments
1envelope = pn.message_counts() \
2 .channel('unique_1') \
3 .channel_timetokens([15510391957007182]) \
4 .sync() \
5print(envelope.result.channels['unique_1'])
1envelope = 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
1envelope = pn.message_counts() \
2 .channel('unique_1,unique_100') \
3 .channel_timetokens([15510391957007182, 15510391957007184]) \
4 .sync()
5print(envelope.result.channels)
1envelope = pubnub.message_counts(channels="unique_1,unique_100",
2 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.
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
reversetoTrue. - Page through results by providing a
startORendtimetoken. - Retrieve a slice of the time line by providing both a
startANDendtimetoken. - Limit the number of messages to a specific quantity using the
countparameter.
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:
1pubnub.history() \
2 .channel(String) \
3 .include_meta(True) \
4 .reverse(Boolean) \
5 .include_timetoken(Boolean) \
6 .start(Integer) \
7 .end(Integer) \
8 .count(Integer)
| Parameter | Description |
|---|---|
channel *Type: String Default: n/a | Specifies channel to return history messages from. |
include_metaType: Boolean Default: False | Specifies whether or not the message's meta information should be returned. |
reverseType: Boolean Default: False | Setting to True will traverse the time line in reverse starting with the oldest message first. |
include_timetokenType: Boolean Default: False | Whether event dates timetokens should be included in response or not. |
startType: Integer Default: n/a | Timetoken delimiting the start of time slice (exclusive) to pull messages from. |
endType: Integer Default: n/a | Timetoken delimiting the end of time slice (inclusive) to pull messages from. |
countType: Integer Default: n/a | 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.
Sample code
Retrieve the last 100 messages on a channel:
1envelope = pubnub.history() \
2 .channel("history_channel") \
3 .count(100) \
4 .sync()
Returns
The history() operation returns a PNHistoryResult which contains the following fields:
| Method | Description |
|---|---|
messagesType: List | List of messages of type PNHistoryItemResult. See PNHistoryItemResult for more details. |
start_timetokenType: Integer | Start timetoken. |
end_timetokenType: Integer | End timetoken. |
PNHistoryItemResult
| Method | Description |
|---|---|
timetokenType: Integer | 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 = pubnub.history() \
2 .channel("my_channel") \
3 .count(3) \
4 .reverse(True) \
5 .sync()
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)
1pubnub.history()\
2 .channel("my_channel")\
3 .start(13847168620721752)\
4 .reverse(True)\
5 .sync()
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)
1pubnub.history()\
2 .channel("my_channel")\
3 .count(100)\
4 .start(-1)\
5 .end(13847168819178600)\
6 .reverse(True)\
7 .sync()
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.
1def get_all_messages(start_tt):
2 def history_callback(result, status):
3 msgs = result.messages
4 start = result.start_timetoken
5 end = result.end_timetoken
6 count = len(msgs)
7
8 if count > 0:
9 print("%d" % count)
10 print("start %d" % start)
11 print("end %d" % end)
12
13 if count == 100:
14 get_all_messages(start)
15
show all 22 linesInclude timetoken in history response
1pubnub.history()\
2 .channel("my_channel")\
3 .count(100)\
4 .include_timetoken()
5 .sync()