This is the first time I am posting here, so please go easy on me : ).
I am also writing for the first time python code which uses async / await for streaming web-socket information.
My main goal is to:
- Connect to a web-socket
- Stream info from web-socket
- Continuously save the streaming info somewhere (to DB for example).
My main concerns are:
- Whether the code below is streaming information from a web-socket correctly - to be clear: the code is working, but I am not 100% certain it's doing what I want it to do, albeit showing results as expected. There's a lot happening under the hood, hence my concern.
- Whether it's also closing the connection to the web-socket.
- How to wrap a function over it (i.e. the function that save the data somewhere).
If you see other problems with it, please let me know.
msg = {'message': 'subscribe'}
async def stream_websocket_info():
async with websockets.connect(WEBSOCKET_URL) as websocket:
await websocket.send(json.dumps(MESSAGE))
while True:
print(await websocket.recv())
# This is how I run it:
asyncio.get_event_loop().run_until_complete(stream_websocket_info())