SelectPython-Tornado Storage & Playback API Reference for Realtime Apps

These docs are for version 3.9.0 of the Python-Tornado SDK. To check out the docs for the latest version Click Here.
 

PubNub 4.0 for Python-Tornado is our latest and greatest! Please click here for our PubNub 4.0 for Python docs and SDK.

PubNub 3.x for Python-Tornado will encounter End Of Life (EOL) July 1st, 2017. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

If you have questions about the PubNub for Python SDK, please contact us at support@pubnub.com.

The PubNub Storage and Playback Service provides realtime 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 day, 7 days, 1 month, and Forever.


Requires Storage & Playback add-on XRequires that the Storage & Playback 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-.
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 to True.
  • Page through results by providing a start OR end time token.
  • Retrieve a slice of the time line by providing both a start AND end time token.
  • 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.
To run History you can use the following method(s) in the Python SDK
  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies channel to return history messages from.
    countIntOptionalSpecifies the number of historical messages to return.

    default/maximum is 100.
    reverseBooleanYesSetting to true will traverse the time line in reverse starting with the oldest message first. Default is false. If both start and end arguments are provided, reverse is ignored and messages are returned starting with the newest message
    callbackFunctionOptionalA callback method should be passed to the method. If set, the api works in async mode. Required argument when working with twisted or tornado.
    errorFunctionOptionalAn error method can be passed to the method. If set, the api works in async mode. Required argument when working with twisted or tornado.
    include_tokenBooleanOptionalWith include_token parameter set to true each envelope will contain timetoken specific for message that it holds.

    Default: false
 
Using the reverse parameter:
Messages are always returned sorted in ascending time direction from Storage/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.
Retrieve the last 100 messages on a channel:
 def _callback(message, channel):
   print(message)
 
 def _error(message):
     print(message)
 
 pubnub.history(channel="history_channel", count=100, callback=_callback, error=_error)
The Python SDK returns false on fail. An array is returned on success. The history() function returns a list of up to 100 messages, the time token of the first (oldest) message and the time token of the last (newest) message in the resulting set of messages. The output below demonstrates the format for a history() response:

[
	["message1", "message2", "message3",... ],
	"Start Time Token",
	"End Time Token"
]
  1. # Sync Example
    print(pubnub.history(channel='my_channel', count=3, reverse=True))
     
    # Async Example
     
    def _callback(message):
        print(message)
     
    def _error(error):
        print(error)
     
    pubnub.history(channel='my_channel', count=3, reverse=True, callback=_callback, error=_error)
    
     [
    	["Pub1","Pub2","Pub3"],
    	13406746729185766,
    	13406746780720711
    ]
    
  2. # Sync Example
    print(pubnub.history(channel='my_channel', start=13406746780720711, reverse=True))
     
    # Async Example
     
    def _callback(message):
        print(message)
     
    def _error(error):
        print(error)
     
    pubnub.history(channel='my_channel', start=13406746780720711, reverse=True, callback=_callback, error=_error)
    
     [
    	["Pub3","Pub4","Pub5"],
    	13406746780720711,
    	13406746845892666
    ]
    
  3. # Sync Example
    print(pubnub.history(channel='demo', end=13406746780720711))
     
    # Async Example
     
    def _callback(message):
        print(message)
     
    def _error(error):
        print(error)
     
    pubnub.history(channel='my_channel', end=13406746780720711,  callback=_callback, error=_error)
     
    
    [
    	["Pub3","Pub4","Pub5"],
    	13406746780720711,
    	13406746845892666
    ]
    
  4.  
    Usage!
    You can call the method by passing 0 or a valid time token as the argument.
    def get_all_messages(timetoken=None):
     
        def _callback(payload):
            msgs    = payload[0]
            start   = payload[1]
            end     = payload[2]
     
            if msgs is not None and len(msgs) > 0:
                print(len(msgs))
                print('Start: ' + str(start))
                print('End: ' + str(end))
     
            if len(msgs) == 100:
                get_all_messages(start)
     
        pubnub.history(channel='history_test', start=timetoken, callback=_callback)
     
    #Examples
     
    get_all_messages()
    get_all_messages(None)
    get_all_messges(14264873967138188)