from fastapi import FastAPI, WebSocket app = FastAPI() worker_ws = None client_ws = None @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): global client_ws await websocket.accept() client_ws = websocket while True: data = await websocket.receive_json() if type(worker_ws) is WebSocket: await worker_ws.send_json(data) else: await websocket.send_text(f"Worker is sleeping now!") @app.websocket("/worker/ws") async def websocket_endpoint(websocket: WebSocket): global worker_ws await websocket.accept() worker_ws = websocket while True: data = await websocket.receive_json() if type(client_ws) is WebSocket: await client_ws.send_json(data) else: await websocket.send_text(f"Client is sleeping now!")