I'm using python websockets: https://websockets.readthedocs.io/
They have a simple client/server example, where the server echoes the client's input back once. The code looks like this:
Client side:
# WS client example import asyncio import websockets async def hello(): async with websockets.connect( 'ws://localhost:8765') as websocket: name = input("What's your name? ") await websocket.send(name) print(f"> {name}") greeting = await websocket.recv() print(f"< {greeting}") asyncio.get_event_loop().run_until_complete(hello()) Server side:
# WS server example import asyncio import websockets async def hello(websocket, path): name = await websocket.recv() print(f"< {name}") greeting = f"Hello {name}!" await websocket.send(greeting) print(f"> {greeting}") start_server = websockets.serve(hello, 'localhost', 8765) asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_forever() I want to adapt just the server side only, so that it does the following upon a socket connection:
- Send an acknowledgement message to the client. e.g.
Hello Client! Please wait for your data. - Keep the connection alive.
- Process some data that takes some time.
- After the data has completed processing, notify the client on the existing websocket connection. e.g.
Your data is here!
The python websockets documentation doesn't have a code sample which does this.