Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # publish
- import time
- import logging
- from pubnub.pnconfiguration import PNConfiguration
- from pubnub.pubnub import PubNub
- from pubnub.exceptions import PubNubException
- from pubnub.callbacks import SubscribeCallback
- from pubnub.enums import PNOperationType, PNStatusCategory
- # pubnub.set_stream_logger('pubnub', logging.DEBUG)
- pnconfig = PNConfiguration()
- pnconfig.publish_key = "demo"
- pnconfig.subscribe_key = "demo"
- pubnub = PubNub(pnconfig)
- for i in range(10):
- time.sleep(3)
- try:
- envelope = pubnub.publish().channel("my_channel").message({
- 'doc_id': '544grhq3277889988ghj',
- 'status': "preview",
- 'count': {"all":1, "pending":4}
- }).sync()
- print("publish timetoken: %d" % envelope.result.timetoken)
- except PubNubException as e:
- print(str(e))
- class MySubscribeCallback(SubscribeCallback):
- def status(self, pubnub, status):
- # The status object returned is always related to subscribe but could contain
- # information about subscribe, heartbeat, or errors
- # use the operationType to switch on different options
- if status.operation == PNOperationType.PNSubscribeOperation \
- or status.operation == PNOperationType.PNUnsubscribeOperation:
- if status.category == PNStatusCategory.PNConnectedCategory:
- print('testing')
- # This is expected for a subscribe, this means there is no error or issue whatsoever
- elif status.category == PNStatusCategory.PNReconnectedCategory:
- pass
- # This usually occurs if subscribe temporarily fails but reconnects. This means
- # there was an error but there is no longer any issue
- elif status.category == PNStatusCategory.PNDisconnectedCategory:
- pass
- # This is the expected category for an unsubscribe. This means there
- # was no error in unsubscribing from everything
- elif status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
- pass
- # This is usually an issue with the internet connection, this is an error, handle
- # appropriately retry will be called automatically
- elif status.category == PNStatusCategory.PNAccessDeniedCategory:
- pass
- # This means that PAM does allow this client to subscribe to this
- # channel and channel group configuration. This is another explicit error
- else:
- pass
- # This is usually an issue with the internet connection, this is an error, handle appropriately
- # retry will be called automatically
- elif status.operation == PNOperationType.PNSubscribeOperation:
- # Heartbeat operations can in fact have errors, so it is important to check first for an error.
- # For more information on how to configure heartbeat notifications through the status
- # PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
- if status.is_error():
- print('het is message')
- # There was an error with the heartbeat operation, handle here
- else:
- print("status error")
- # Heartbeat operation was successful
- else:
- pass
- # Encountered unknown status type
- def presence(self, pubnub, presence):
- print(presence)
- def message(self, pubnub, message):
- print(message.message)
- pubnub.add_listener(MySubscribeCallback())
- pubnub.subscribe().channels("5c88a695b87d2d0c5258177f").execute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement