TwistedTwistedTornadoAsyncioNativePythonPython-Twisted V4 Configuration API Reference for Realtime Apps

 

NOTICE: Based on current web trends and our own usage data, PubNub's Python Twisted SDK is deprecated as of May 1, 2019. Deprecation means we will no longer be updating the Python Twisted SDK but will continue to support users currently using it. Please feel free to use our other Python SDK offerings as they will continue to be supported and maintained. If you would like to use the Python Twisted SDK specifically, we would love to work with you on keeping this project alive!

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

PNConfiguration instance is storage for user-provided information which describe further PubNub client behaviour. Configuration instance contain additional set of properties which allow to perform precise PubNub client configuration.
To create configuration instance you can use the following function in the Python-Twisted V4 SDK:
  1. PropertiesTypeRequiredDefaultsDescription
    subscribe_keyStringYessubscribe_key from admin panel.
    publish_keyStringOptionalNonepublish_key from admin panel (only required if publishing).
    secret_keyStringOptionalNonesecret_key (only required for modifying/revealing access permissions)
    cipher_keyStringOptionalNoneIf cipher_key is passed, all communications to/from PubNub will be encrypted.
    uuidStringOptionalSDK generated uuidUUID to be used as a device identifier, a default UUID is generated if not passed.
    auth_keyStringOptionalNoneIf Access Manager is utilized, client will use this authKey in all restricted requests.
    sslBooleanOptionalFalseUse SSL.
    connect_timeoutIntOptional5

    How long to wait before giving up connection to client.


    The value is in seconds.

    subscribe_timeoutIntOptional310

    How long to keep the subscribe loop running before disconnect.


    The value is in seconds.

    non_subscribe_request_timeoutIntOptional10

    On non subscribe operations, how long to wait for server response.


    The value is in seconds.

    filter_expressionStringOptionalNoneFeature to subscribe with a custom filter expression.
    heartbeat_notification_optionsPNHeartbeatNotificationOptionsOptionalPNHeartbeatNotificationOptions.FAILURES

    Heartbeat notifications, by default, the SDK will alert on failed heartbeats (equivalent to: PNHeartbeatNotificationOptions.FAILURES).

    Other options such as all heartbeats (PNHeartbeatNotificationOptions.ALL) or no heartbeats (PNHeartbeatNotificationOptions.NONE) are supported.

    reconnect_policyPNReconnectionPolicyOptionalPNReconnectionPolicy.NONE

    Set to PNReconnectionPolicy.LINEAR for automatic reconnects. Use option PNReconnectionPolicy.NONE to disable automatic reconnects.


    Use option PNReconnectionPolicy.EXPONENTIAL to set exponential retry interval.

    originStringOptionalps.pndsn.comCustom origin if needed.
    enable_subscribeBooleanOptionalTrueYou can disable the subscribe loop if you don't need perform subscribe operations. By default subscribe loop is enabled and extra threads/loops are started. They should be explicitly stopped by pubnub.stop() method invocation.
    daemonBooleanOptionalFalseWhen set to True, spawned thread won't keep the application running after SIGTERM. (ctrl-c from command line, for example)
from pubnub.pnconfiguration import PNConfiguration

pn_configuration = PNConfiguration()
# subscribe_key from admin panel
pn_configuration.subscribe_key = "my_subscribe_key" // required
# publish_key from admin panel (only required if publishing)
pn_configuration.publish_key = "my_publish_key"
# secret_key (only required for modifying/revealing access permissions)
pn_configuration.secret_key = "my_secret_key"
# if cipher_key is passed, all communicatons to/from pubnub will be encrypted
pn_configuration.cipher_key = "my_cipher_key"
# UUID to be used as a device identifier, a default UUID is generated
# if not passed
pn_configuration.uuid = "my_custom_uuid"
# if Access Manager is utilized, client will use this authKey in all restricted
# requests
pn_configuration.auth_key = "my_aut_key"
# use SSL
pn_configuration.ssl = True
# how long to wait before giving up connection to client
pn_configuration.connect_timeout = 100
# how long to keep the subscribe loop running before disconnect
pn_configuration.subscribe_timeout = 310
# on non subscribe operations, how long to wait for server response
pn_configuration.non_subscribe_timeout = 300
# PSV2 feature to subscribe with a custom filter expression
pn_configuration.filter_expression = "such=wow"
# heartbeat notifications, by default, the SDK will alert on failed heartbeats.
# other options such as all heartbeats or no heartbeats are supported.
pn_configuration.heartbeat_notification_options = PNHeartbeatNotificationOptions.All
Add PubNub to your project using one of the procedures defined under How to Get It.
  • PyOpenSSL
  • Service-Identity
from pubnub.pubnub_twisted import PubNubTwisted
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.
 
You must call start() to initiate the PubNub Client API in Python-Twisted V4.
To Initialize Pubnub you can use the following method(s) in the Python-Twisted V4 SDK:
  1. ParameterTypeRequiredDescription
    pn_configurationPNConfigurationYesGoto Configuration for more details.
from pubnub.pubnub_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subkey"
pnconfig.publish_key = "my_pubkey"
pnconfig.ssl = True

pubnub = PubNubTwisted(pnconfig)
It returns the Pubnub instance for invoking PubNub APIs like publish(), subscribe(), history(), here_now(), etc.
  1. from pubnub.pubnub_twisted import PubNubTwisted
    from pubnub.pnconfiguration import PNConfiguration
    
    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = "my_subkey"
    pnconfig.publish_key = "my_pubkey"
    pnconfig.ssl = False
    
    pubnub = PubNubTwisted(pnconfig)
  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:
    from pubnub.pubnub_twisted import PubNubTwisted
    from pubnub.pnconfiguration import PNConfiguration
    
    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = "my_subkey"
    
    pubnub = PubNubTwisted(pnconfig)
  3. Set a custom UUID to identify your users.
    from pubnub.pubnub_twisted import PubNubTwisted
    from pubnub.pnconfiguration import PNConfiguration
    
    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = 'demo'
    pnconfig.publish_key = 'demo'
    pnconfig.uuid = 'Stephen'
    
    pubnub = PubNubTwisted(pnconfig)
  4. This examples demonstrates how to enable PubNub Transport Layer Encryption with SSL. Just initialize the client with ssl 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.

    from pubnub.pubnub_twisted import PubNubTwisted
    from pubnub.pnconfiguration import PNConfiguration
    
    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = "my_subkey"
    pnconfig.publish_key = "my_pubkey"
    pnconfig.ssl = True
    
    pubnub = PubNubTwisted(pnconfig)
  5. 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:

    http://www.pubnub.com/knowledge-base/discussion/644/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:
    from pubnub.pubnub_twisted import PubNubTwisted
    from pubnub.pnconfiguration import PNConfiguration
    
    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = "my_subkey"
    pnconfig.publish_key = "my_pubkey"
    pnconfig.secret_key = "my_secretkey"
    pnconfig.ssl = True
    
    pubnub = PubNubTwisted(pnconfig)
    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.
  6. from pubnub.pubnub_twisted import PubNubTwisted
    from pubnub.pnconfiguration import PNConfiguration
    
    pnconfig = PNConfiguration()
    pnconfig.subscribe_key = "my_subkey"
    pnconfig.publish_key = "my_pubkey"
    
    pubnub = PubNubTwisted(pnconfig, custom_ioloop=my_event_loop)
Requires Stream Controller add-on XRequires that the Stream Controller add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
To set/get filters you can use the following methods.
  1. The property accepts a string.
  2. The property returns a string

from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.filter_expression = "such=wow"
filter = pnconfig.filter_expression

Go to Publish & Subscribe