SelectPubNub Python SDK 3.9.0

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

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

PubNub 3.x for Python 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 simplest way to get started is to install PubNub Python SDK via pypi:

pip install 'pubnub>=3,<4'
git clone https://github.com/pubnub/python



Add PubNub to your project using one of the procedures defined under How to Get It.
from pubnub import Pubnub
pubnub = Pubnub(publish_key="demo", subscribe_key="demo")
def callback(message, channel):
    print(message)
 
 
def error(message):
    print("ERROR : " + str(message))
 
 
def connect(message):
    print("CONNECTED")
    print pubnub.publish(channel='my_channel', message='Hello from the PubNub Python SDK')
 
 
 
def reconnect(message):
    print("RECONNECTED")
 
 
def disconnect(message):
    print("DISCONNECTED")
 
 
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.

pubnub = Pubnub(publish_key='demo', subscribe_key='demo')
Call time() to verify the client connectivity to the origin:
def _callback(response):
    print(response)
pubnub.time(callback=_callback)
Subscribe (listen on) a channel (it's async!):
 
 def _callback(message, channel):
   print(message)
 
 def _error(message):
     print(message)
 
 pubnub.subscribe(channels="my_channel", callback=_callback, error=_error)
Publish a message to a channel:
# Publish a welcome message
def callback(message):
	print(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 that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
 def _callback(message, channel):
   print(message)
 
 def _error(message):
     print(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 that the Presence add-on is enabled for your key. How do I enable add-on features for my keys? - see http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
def _callback(message, channel):
    print(message)

def _error(message):
    print(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 http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys
 def _callback(message, channel):
   print(message)
 
 def _error(message):
     print(message)
 
 pubnub.history(channel="history_channel", count=100, callback=_callback, error=_error)
Stop subscribing (listening) to a channel.
pubnub.unsubscribe(channel='my_channel')