SelectPython-Tornado Configuration 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.

Python complete API reference for building Realtime Applications on PubNub, including basic usage and sample code.

Add PubNub to your project using one of the procedures defined under How to Get It.
 
Be sure to eventually start the event loop or PubNub won't run!

pubnub.start()
from pubnub import PubnubTornado as Pubnub
This function is used for initializing the PubNub Client API context. This function must be called before attempting to utilize any API functionality in order to establish account level credentials such as publish_key and subscribe_key.

 
If the purpose of the application is standalone, daemon must be set to True to ensure graceful application termination.
To Initialize Pubnub you can use the following method(s) in the Python SDK:
  1. ParameterTypeRequiredDescription
    publish_keyStringOptionalSpecifies the required publish_key to be used only when publishing messages to a channel. This key can be specified at initialization or along with a publish() and need not be specified if the client will not publish data to any channel.
    subscribe_keyStringYesSpecifies the required subscribe_key to be used for subscribing to a channel.
    secret_KeyStringOptionalSpecifies the secret key.

    Default: None
    cipher_KeyStringOptionalSpecifies the cipher key.

    Default: None
    auth_KeyStringOptionalSpecifies the auth key.

    Default: None
    sslOnStringOptionalSetting a value of True enables transport layer encryption with SSL/TLS.

    Default: False.
    originStringOptionalDefault: ps.pndsn.com
    uuidStringYes

    UUID to use. You should set a unique UUID to identify the user or the device that connects to PubNub.

    Allowing the SDK to generate a random UUID can result in significant billing impacts, particularly on an MAU pricing plan.

    poolingBooleanOptionalDefault: True
    daemonBooleanOptionalDefault: False
    pres_uuidStringOptionalDefault: None
    azureBooleanOptionalDefault: False
 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
pubnub = Pubnub(publish_key='my_pubkey', subscribe_key='my_subkey',ssl_on=True)
It returns the Pubnub instance for invoking PubNub APIs like publish(), subscribe(), history(), here_now, etc.
  1.  Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    pubnub = Pubnub(publish_key='demo', subscribe_key='demo')
  2. In the case where a client will only read messages and never publish to a channel, you can simply omit the publish_key when initializing the client:
     Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    pubnub = Pubnub(publish_key='', subscribe_key='demo') 
  3. Set a custom UUID to identify your users.
     Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    pubnub = Pubnub(publish_key='myPublishKey', subscribe_key='mySubscribeKey', uuid='myUniqueUUID')
  4. It is possible to define custom logging and actions through the use of callbacks:
     Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    pubnub = Pubnub(publish_key='demo', subscribe_key='demo')
     
     def _callback(message, channel):
       print(message)
     
     def _error(message):
         print(message)
     
     pubnub.subscribe(channels="my_channel", callback=_callback, error=_error)
  5. This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with ssl_on set to True. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

     Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    pubnub = Pubnub(publish_key='my_pubkey', subscribe_key='my_subkey',ssl_on=True)
  6. Requires Access Manager add-on XRequires that the Access Manager 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-.
     

    Anyone with the secret_key can grant and revoke permissions to your app. Never let your secret key be discovered, and to only exchange it / deliver it securely. Only use the secret_key on secure server-side platforms.

    When you init with secret_key, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.

    For applications that will administer PAM permissions, the API is initialized with the secret_key as in the following example:
     Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    pubnub = Pubnub(publish_key='', subscribe_key='my_subkey', secret_key ='my_secretkey')
    Now that the pubnub object is instantiated the client will be able to access the PAM functions. The pubnub object will use the secret_key to sign all PAM messages to the PubNub Network.
This function is used to set a user ID on the fly.
To set UUID you can use the following method(s) in Python SDK:
 Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
uuid.uuid4()
  1. import uuid
    unique_channel = str(uuid.uuid4())
     
    def callback(message, channel_group):
        print(message)
     
     
    def error(message):
        print("ERROR : " + str(message))
     
     
    def connect(message):
        print("CONNECTED")
        print pubnub.publish(channel= unique_channel, message='hi')
     
     
     
    def reconnect(message):
        print("RECONNECTED")
     
     
    def disconnect(message):
        print("DISCONNECTED")
     
     
    pubnub.subscribe_group(channels=unique_channel, callback=callback, error=callback,
                     connect=connect, reconnect=reconnect, disconnect=disconnect)
  2.  Always set the UUID to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing if your account uses the Monthly Active Users (MAUs) based pricing model, and can also lead to unexpected behavior if you have Presence enabled.
    import random
    uuid = uuid + str(random.random())
     
    pubnub = Pubnub(publish_key='myPublishKey', subscribe_key='mySubscribeKey', uuid=uuid)
  3. import uuid
    auth_key = str(uuid.uuid4())
    
    
Setter and getter for users auth key.
  1. ParameterTypeRequiredDescription
    auth_keyStringYesauth Key
  2. This method doesn't take any arguments.
  3. ParameterTypeRequiredDescription
    auth_keyStringYesuse empty string to unset
pubnub.set_auth_key('my_new_authkey')
auth_key = pubnub.get_auth_key()
pubnub.set_auth_key('')
Get Auth key returns the current authentication key.