Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save richiejarvis/1dd18c1e905df850d508959c6b348d7e to your computer and use it in GitHub Desktop.
Save richiejarvis/1dd18c1e905df850d508959c6b348d7e to your computer and use it in GitHub Desktop.
Example python3 publish via Ably
#!/usr/bin/env python3
from __future__ import print_function
import sys
import os
import time
import asyncio
from ably import AblyRest
my_path = os.path.abspath(__file__) # Find the full path of this python script
# get the path location only (excluding script name)
base_dir = my_path[0:my_path.rfind("/")+1]
base_file_name = my_path[my_path.rfind("/")+1:my_path.rfind(".")]
prog_name = os.path.basename(__file__)
# Check for variable file to import and error out if not found.
config_file_path = os.path.join(base_dir, "config.py")
if not os.path.exists(config_file_path):
print("ERROR : Missing config.py file - File Not Found %s"
% config_file_path)
sys.exit(1)
# Read Configuration variables from config.py file
from config import *
PROG_VER = "ver 0.1"
async def Main():
client = AblyRest(auth_url='http://localhost:3000/auth')
token_details = await client.auth.request_token(token_params={'client_id': 'bob'})
print("Success; token = " + str(token_details.token))
for counter in range(10):
record = {
'line1': "line_one_contents" + str(counter),
'line2': "line_two_contents" + str(counter)
}
new_client = AblyRest(token=token_details.token)
channel = new_client.channels.get('testchan')
await channel.publish("event",record)
print("Published:" + str(record))
await new_client.close()
await client.close()
if __name__ == "__main__":
asyncio.run(Main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment