Anif Zarus
Anif Zarus

Reputation: 45

pubnub python 4 sdk

I have just started using pubnub. I entered the basic code which was given in pubnub python sdk (4.0) and I get the following errors

ERROR:pubnub:Async request Exception. 'Publish' object has no attribute 'async' ERROR:pubnub:Exception in subscribe loop: 'Publish' object has no attribute 'async' WARNING:pubnub:reconnection policy is disabled, please handle reconnection manually.

As far as the async() is concerned, there is a troubleshoot in which the async error can be solved be entering the following

def callback(result, status):
    if status.is_error():
        print("Error %s" % str(status.error_data.exception))
        print("Error category #%d" % status.category)
    else:
        print(str(result))\

but still it doesn't work.

This is the code

 from pubnub.callbacks import SubscribeCallback

 from pubnub.enums import PNStatusCategory

 from pubnub.pnconfiguration import PNConfiguration

 from pubnub.pubnub import PubNub

 pnconfig = PNConfiguration()

 pnconfig.subscribe_key = 'demo'
 pnconfig.publish_key = 'demo'

 pubnub = PubNub(pnconfig)


def my_publish_callback(envelope, status):
      # Check whether request successfully completed or not
      if not status.is_error():
          pass  # Message successfully published to specified channel.
      else:
          pass  # Handle message publish error. Check 'category' property to find out possible issue
    # because of which request did fail.
    # Request can be resent using: [status retry];


 class MySubscribeCallback(SubscribeCallback):
      def presence(self, pubnub, presence):
          pass  # handle incoming presence data

      def status(self, pubnub, status):
        if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
           pass  # This event happens when radio / connectivity is lost

    elif status.category == PNStatusCategory.PNConnectedCategory:
        # Connect event. You can do stuff like publish, and know you'll get it.
        # Or just use the connected event to confirm you are subscribed for
        # UI / internal notifications, etc
        pubnub.publish().channel("awesomeChannel").message("hello!!").async(my_publish_callback)
        elif status.category == PNStatusCategory.PNReconnectedCategory:
           pass
        # Happens as part of our regular operation. This event happens when
        # radio / connectivity is lost, then regained.
        elif status.category == PNStatusCategory.PNDecryptionErrorCategory:
        pass
        # Handle message decryption error. Probably client configured to
        # encrypt messages and on live data feed it received plain text.

def message(self, pubnub, message):
    pass  # Handle new message stored in message.message


pubnub.add_listener(MySubscribeCallback())
pubnub.subscribe().channels('awesomeChannel').execute()

Upvotes: 3

Views: 543

Answers (1)

R.W
R.W

Reputation: 560

As the error is from the publish method, it could most probably be because async has been changed to pn_async

Note that as on date, this is applicable only for Python3 as the same has not been implemented for Python 2.

Change

pubnub.publish().channel("awesomeChannel").message("hello!!").async(my_publish_callback)

to

pubnub.publish().channel("awesomeChannel").message("hello!!").pn_async(my_publish_callback)

Reference document here

Upvotes: 2

Related Questions