Created
June 21, 2022 15:15
-
-
Save Ugbot/f7bd307e74a804a1529c8f63bc597aa2 to your computer and use it in GitHub Desktop.
An async example of mqtt from ably realtime in python
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
async def ably_mqtt(): | |
async with Client( | |
hostname='mqtt.ably.io', | |
username="***************", | |
password="**************" | |
port=1883, | |
# protocol=ProtocolVersion.V31 | |
) as client: | |
async with client.unfiltered_messages() as messages: | |
# subscribe is done afterwards so that we just start receiving messages | |
# from this point on | |
await client.subscribe("[product:ably-coindesk/crypto-pricing?rewind=100]eth:usd") | |
# await client.subscribe("[product:ably-coindesk/crypto-pricing]btc:usd") | |
async for message in messages: | |
print(message.topic) | |
print(message.payload) | |
print(message.payload.decode()) | |
async def main(): | |
# Run the advanced_example indefinitely. Reconnect automatically | |
# if the connection is lost. | |
reconnect_interval = 3 # [seconds] | |
while True: | |
try: | |
await ably_mqtt() | |
except MqttError as error: | |
print(f'Error "{error}". Reconnecting in {reconnect_interval} seconds.') | |
finally: | |
await asyncio.sleep(reconnect_interval) | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment