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 support@pubnub.com.

  • 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):
        print("Connected")
        pubnub.publish(channel='the_channel', message='data')
    
    
    def reconnect(message):
        print("Reconnected")
    
    
    def disconnect(message):
        print("Disconnected")
    
    
    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 http://www.pubnub.com/docs/python/api-reference-publish-and-subscribe#listeners
    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.')
                pubnub.publish().channel('the_channel').message('data').sync()
    
        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()
    pn.add_listener(handler)
    pn.subscribe().channels('the_channel').execute()
  • V3 accepts callback and error callbacks as arguments:
    def callback(result):
        print(result)
    
    def errback(error):
        print(error)
    
    pubnub.publish('ch', 'Hi', callback=callback, error=errback)
  • V4 sync way
    try:
        pubnub.publish().channel('ch').message('Hi').sync()
    except PubNubException as e:
        print(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.