I have a basic websockets server that runs continuously and sends a message whenever a client connects:
import asyncio
import websockets
async def handler(websocket, path):
print("New connection established")
await websocket.send("Hello from the server!")
async def start_server():
async with websockets.serve(handler, "localhost", 8765):
await asyncio.Future()
asyncio.run(start_server())
I want to have a function that sends a message whenever it is called (basically when the rest of the program processes some data in the main loop), all this while the server stays alive. For example:
...do stuff...
message = "Processed data"
websocket_send_message(message)