SelectPython-Twisted SDK 3.9.0

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

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

PubNub 3.x for Python-Twisted 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

The simplest way to get started is to install PubNub Python SDK via pypi:

pip install 'pubnub>=3,<4'
git clone && cd python/python-twisted

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!

from pubnub import PubnubTwisted as Pubnub
pubnub = Pubnub(publish_key="demo", subscribe_key="demo")
def callback(message, channel):
def error(message):
    print("ERROR : " + str(message))
def connect(message):
    print pubnub.publish(channel='my_channel', message='Hello from the PubNub Python SDK')
def reconnect(message):
def disconnect(message):
pubnub.subscribe(channels='my_channel', callback=callback, error=callback,
                 connect=connect, reconnect=reconnect, disconnect=disconnect)
In addition to the Hello World sample code, we also provide some copy and paste snippets of common API functions:
Instantiate a new Pubnub instance. Only the subscribe_key is mandatory. Also include publish_key if you intend to publish from this instance, and the secret_key if you wish to perform PAM administrative operations from this Python instance.

For security reasons you should only include the secret-key on a highly secured server. The secret-key is only required for granting rights using our Access Manager.

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.

 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')
Call time() to verify the client connectivity to the origin:
def _callback(response):
Subscribe (listen on) a channel (it's async!):
 def _callback(message, channel):
 def _error(message):
 pubnub.subscribe(channels="my_channel", callback=_callback, error=_error)
Publish a message to a channel:
# Publish a welcome message
def callback(message):

pubnub.publish('my_channel', 'Hello from PubNub Python SDK!', callback=callback, error=callback)
Get occupancy of who's here now on the channel by UUID:
Requires you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
 def _callback(message, channel):
 def _error(message):
 pubnub.here_now(channel="my_channel", callback=_callback, error=_error)
Subscribe to realtime Presence events, such as join, leave, and timeout, by UUID. Setting the presence attribute to a callback will subscribe to presents events on my_channel:
Requires you to enable the Presence add-on for your key. Refer to the How do I enable add-on features for my keys? knowledge base article for details on enabling features.
def _callback(message, channel):

def _error(message):

pubnub.presence(channel="my_channel", callback=_callback, error=_error)
Retrieve published messages from archival storage:
Requires that the Storage and Playback add-on is enabled for your key. How do I enable add-on features for my keys? - see
 def _callback(message, channel):
 def _error(message):
 pubnub.history(channel="history_channel", count=100, callback=_callback, error=_error)
Stop subscribing (listening) to a channel.