Skip to content

Commit

Permalink
added simple AsyncIO example. (#188)
Browse files Browse the repository at this point in the history
* added simple AsyncIO example.

* added gif to README.md file.

* Update main.py to satisfy linter

---------

Co-authored-by: Sebastian Molenda <[email protected]>
  • Loading branch information
stephenlb and seba-aln authored Jul 30, 2024
1 parent c3c573d commit e5416cf
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/pubnub_asyncio_simple/README.md
Original file line number Diff line number Diff line change
@@ -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"]
```
52 changes: 52 additions & 0 deletions examples/pubnub_asyncio_simple/main.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit e5416cf

Please sign in to comment.