From e5416cf0c03b35833d1e75f2fcf081d87ca11f15 Mon Sep 17 00:00:00 2001 From: Stephen Blum Date: Tue, 30 Jul 2024 01:51:12 -0700 Subject: [PATCH] added simple AsyncIO example. (#188) * added simple AsyncIO example. * added gif to README.md file. * Update main.py to satisfy linter --------- Co-authored-by: Sebastian Molenda --- examples/pubnub_asyncio_simple/README.md | 35 ++++++++++++++++ examples/pubnub_asyncio_simple/main.py | 52 ++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 examples/pubnub_asyncio_simple/README.md create mode 100644 examples/pubnub_asyncio_simple/main.py diff --git a/examples/pubnub_asyncio_simple/README.md b/examples/pubnub_asyncio_simple/README.md new file mode 100644 index 00000000..497e1988 --- /dev/null +++ b/examples/pubnub_asyncio_simple/README.md @@ -0,0 +1,35 @@ +# AsyncIO PubNub Subscribe Example + +![pubnub-asyncio-simple-example](https://gist.github.com/assets/45214/07223c2e-a5f0-453d-91b2-819fcb526ab5) + +### Usage example: +```shell +pip install asyncio pubnub +export PUBNUB_PUBLISH_KEY=demo +export PUBNUB_SUBSCRIBE_KEY=demo +python main.py +``` + +### Output: +``` +Listening for messages... +Connected +Received message: Hello World on channel: my_channel +Received message: Hello World on channel: my_channel +Received message: Hello World on channel: my_channel +Received message: Hello World on channel: my_channel +Received message: Hello World on channel: my_channel +``` + + +### In another terminal: +```shell +export PUBNUB_PUBLISH_KEY=demo +export PUBNUB_SUBSCRIBE_KEY=demo +curl "https://ps.pndsn.com/publish/${PUBNUB_PUBLISH_KEY}/${PUBNUB_SUBSCRIBE_KEY}/0/my_channel/0/%22Hello%20World%22" +``` + +### Output: +``` +[1,"Sent","17183967137027574"] +``` diff --git a/examples/pubnub_asyncio_simple/main.py b/examples/pubnub_asyncio_simple/main.py new file mode 100644 index 00000000..b7fb893d --- /dev/null +++ b/examples/pubnub_asyncio_simple/main.py @@ -0,0 +1,52 @@ +import os +import asyncio + +from pubnub.pnconfiguration import PNConfiguration +from pubnub.pubnub_asyncio import PubNubAsyncio, SubscribeCallback +from pubnub.enums import PNStatusCategory + + +class MySubscribeCallback(SubscribeCallback): + def status(self, pubnub, status): + if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory: + print("Disconnected") + elif status.category == PNStatusCategory.PNConnectedCategory: + print("Connected") + elif status.category == PNStatusCategory.PNReconnectedCategory: + print("Reconnected") + elif status.category == PNStatusCategory.PNDecryptionErrorCategory: + print("Decryption error") + + def message(self, pubnub, message): + print(f"Received message: {message.message} on channel: {message.channel}") + + def presence(self, pubnub, presence): + print(f"Presence event: {presence.event}") + + +async def main(pubnub): + pubnub.subscribe().channels('my_channel').execute() + print("Listening for messages...") + while True: + await asyncio.sleep(1) + +if __name__ == "__main__": + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + pnconfig = PNConfiguration() + pnconfig.subscribe_key = os.getenv('PUBNUB_SUBSCRIBE_KEY') or 'demo' + pnconfig.publish_key = os.getenv('PUBNUB_PUBLISH_KEY') or 'demo' + pnconfig.user_id = "my_unique_user_id" # Set a unique user ID + + pubnub = PubNubAsyncio(pnconfig) + callback = MySubscribeCallback() + pubnub.add_listener(callback) + + try: + loop.run_until_complete(main(pubnub)) + except KeyboardInterrupt: + print("Interrupted by user. Exiting...") + finally: + loop.run_until_complete(pubnub.stop()) # Assuming 'pubnub' is in scope + loop.close()