NativeNativeTwistedTornadoAsyncioPythonPython V3 to Python V4 Migration Guide


These docs are for PubNub 4.0 for Python V4 which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.0 for Python, PubNub 3.0 for Python-Tornado and PubNub 3.0 for Python-Twisted.

If you have questions about the PubNub for Python SDK, please contact us at

  • V3 exposes Pubnub
  • V4 exposes PubNub
  • V3 initiates via:
    pubnub = Pubnub(publish_key="demo", subscribe_key="demo")
  • V4 initiates via:
    pnconf = PNConfiguration()
    pnconf.subscribe_key = 'demo'
    pnconf.publish_key = 'demo'
    pubnub = PubNub(pnconf)
  • V3 accepts a callback as a parameter to announce when messages come in.
    from pubnub import Pubnub
    def callback(message, channel):
        print('Message:', message, 'Channel:', channel)
    def error(message):
        print("Error:", message)
    def connect(message):
        pubnub.publish(channel='the_channel', message='data')
    def reconnect(message):
    def disconnect(message):
    pubnub = Pubnub(publish_key="demo", subscribe_key="demo")
    pubnub.subscribe(channels='the_channel', callback=callback, error=callback,
                     connect=connect, reconnect=reconnect, disconnect=disconnect)
  • V4 switches to the listener pattern for messages, presence and status. Please refer to the listener documentation for more details
    from pubnub.callbacks import SubscribeCallback
    from pubnub.enums import PNStatusCategory
    from pubnub.pnconfiguration import PNConfiguration
    from pubnub.pubnub import PubNub
    class CustomCallback(SubscribeCallback):
        def status(self, pubnub, status):
            if status.category == PNStatusCategory.PNConnectedCategory:
                print('Publish a sample message on connect.')
        def presence(self, pubnub, presence):
            print('Presence:', presence)  # handle incoming presence data
        def message(self, pubnub, message):
            print('Message:', message)  # handle incoming messages
    config = PNConfiguration()
    config.publish_key = 'demo'
    config.subscribe_key = 'demo'
    pn = PubNub(config)
    handler = CustomCallback()
  • V3 accepts callback and error callbacks as arguments:
    def callback(result):
    def errback(error):
    pubnub.publish('ch', 'Hi', callback=callback, error=errback)
  • V4 sync way
    except PubNubException as e:
  • V3 exposes subscribe callbacks via method params.
  • V4 exposes the functionality via listeners.
  • V4 returns a status object for each transactional request in the envelope and in the status portion of a listener or a callback.