TornadoTornadoTwistedAsyncioNativePythonPython-Tornado V3 to Python-Tornado V4 Migration Guide

 

These docs are for PubNub 4.0 for Python-Tornado 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 PubnubTornado
  • V4 exposes PubNubTornado
  • 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 PubnubTornado
    
    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 = PubnubTornado(publish_key='demo', subscribe_key='demo', secret_key='demo')
    pubnub.subscribe(channels='the_channel', callback=callback, error=error,
                     connect=connect, reconnect=reconnect, disconnect=disconnect)
    pubnub.start()
  • 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-tornado/api-reference-publish-and-subscribe#listeners
    import tornado.ioloop
    
    from pubnub.callbacks import SubscribeCallback
    from pubnub.enums import PNStatusCategory
    from pubnub.pnconfiguration import PNConfiguration
    from pubnub.pubnub_tornado import PubNubTornado
    
    
    def publish_handler(future):
        print(future.result())
    
    
    class CustomCallback(SubscribeCallback):
        def status(self, pubnub, status):
            if status.category == PNStatusCategory.PNConnectedCategory:
                print('Publish a sample message on connect.')
                pubnub.ioloop.add_future(pubnub.publish().channel('the_channel').message('data').future(),
                                         publish_handler)
    
        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 = PubNubTornado(config)
    
    handler = CustomCallback()
    pn.add_listener(handler)
    pn.subscribe().channels('the_channel').execute()
    tornado.ioloop.IOLoop.current().start()
  • 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
    envelope = yield pubnub.publish().channel('ch').message('Hi').future()
    
  • 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.