-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |