SelectUpgrading Python Tornado V3 to V4

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

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

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

  • V3 exposes PubnubTornado
  • V4 exposes PubNubTornado
 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.
  • 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.
     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.
    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
     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.
    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.