daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Mar 25th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # publish
  2. import time
  3. import logging
  4.  
  5. from pubnub.pnconfiguration import PNConfiguration
  6. from pubnub.pubnub import PubNub
  7. from pubnub.exceptions import PubNubException
  8. from pubnub.callbacks import SubscribeCallback
  9. from pubnub.enums import PNOperationType, PNStatusCategory
  10.  
  11.  
  12. # pubnub.set_stream_logger('pubnub', logging.DEBUG)
  13.  
  14. pnconfig = PNConfiguration()
  15.  
  16. pnconfig.publish_key = "demo"
  17. pnconfig.subscribe_key = "demo"
  18.  
  19. pubnub = PubNub(pnconfig)
  20.  
  21. for i in range(10):
  22.     time.sleep(3)
  23.     try:
  24.         envelope = pubnub.publish().channel("my_channel").message({
  25.             'doc_id': '544grhq3277889988ghj',
  26.             'status': "preview",
  27.             'count': {"all":1, "pending":4}
  28.         }).sync()
  29.         print("publish timetoken: %d" % envelope.result.timetoken)
  30.     except PubNubException as e:
  31.         print(str(e))
  32.  
  33.  
  34. class MySubscribeCallback(SubscribeCallback):
  35.     def status(self, pubnub, status):
  36.         # The status object returned is always related to subscribe but could contain
  37.         # information about subscribe, heartbeat, or errors
  38.         # use the operationType to switch on different options
  39.         if status.operation == PNOperationType.PNSubscribeOperation \
  40.                 or status.operation == PNOperationType.PNUnsubscribeOperation:
  41.             if status.category == PNStatusCategory.PNConnectedCategory:
  42.                 print('testing')
  43.                 # This is expected for a subscribe, this means there is no error or issue whatsoever
  44.             elif status.category == PNStatusCategory.PNReconnectedCategory:
  45.                 pass
  46.                 # This usually occurs if subscribe temporarily fails but reconnects. This means
  47.                 # there was an error but there is no longer any issue
  48.             elif status.category == PNStatusCategory.PNDisconnectedCategory:
  49.                 pass
  50.                 # This is the expected category for an unsubscribe. This means there
  51.                 # was no error in unsubscribing from everything
  52.             elif status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
  53.                 pass
  54.                 # This is usually an issue with the internet connection, this is an error, handle
  55.                 # appropriately retry will be called automatically
  56.             elif status.category == PNStatusCategory.PNAccessDeniedCategory:
  57.                 pass
  58.                 # This means that PAM does allow this client to subscribe to this
  59.                 # channel and channel group configuration. This is another explicit error
  60.             else:
  61.                 pass
  62.                 # This is usually an issue with the internet connection, this is an error, handle appropriately
  63.                 # retry will be called automatically
  64.         elif status.operation == PNOperationType.PNSubscribeOperation:
  65.             # Heartbeat operations can in fact have errors, so it is important to check first for an error.
  66.             # For more information on how to configure heartbeat notifications through the status
  67.             # PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
  68.             if status.is_error():
  69.                 print('het is message')
  70.                 # There was an error with the heartbeat operation, handle here
  71.             else:
  72.                 print("status error")
  73.                 # Heartbeat operation was successful
  74.         else:
  75.             pass
  76.             # Encountered unknown status type
  77.  
  78.     def presence(self, pubnub, presence):
  79.         print(presence)
  80.  
  81.     def message(self, pubnub, message):
  82.         print(message.message)
  83.  
  84.  
  85. pubnub.add_listener(MySubscribeCallback())
  86. pubnub.subscribe().channels("5c88a695b87d2d0c5258177f").execute()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top