Skip to content

Commit

Permalink
Ping from frontend and reduce delay in messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
marySalvi committed Sep 11, 2024
1 parent 6f64725 commit b0ef00d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
7 changes: 7 additions & 0 deletions client/src/HomePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ ws.onmessage = (event) => {
redact_dir: data.redact_dir || progress.value.redact_dir, // don't update if not present
};
};
// Periodically ping the websocket
setInterval(() => {
if (ws.readyState === ws.OPEN) {
ws.send("ping");
}
}, 5000);
const redact_images = async () => {
if (!selectedDirectories.value.inputDirectory || !selectedDirectories.value.outputDirectory) {
Expand Down
14 changes: 2 additions & 12 deletions imagedephi/gui/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ def redact(
redact_images(input_path, output_path)


async def ws_heartbeat(websocket: WebSocket):
while True:
try:
await websocket.send_json("heartbeat")
await asyncio.sleep(10)
except WebSocketDisconnect:
print("Client disconnected")
break


@router.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
Expand All @@ -192,7 +182,6 @@ async def websocket_endpoint(websocket: WebSocket):
print("Client connected")
backoff = 1

asyncio.create_task(ws_heartbeat(websocket))
while True:
message = get_next_progress_message()
if message is not None:
Expand All @@ -201,7 +190,8 @@ async def websocket_endpoint(websocket: WebSocket):
)
await websocket.send_json(message_dict)
else:
await asyncio.sleep(1) # Add a small delay to avoid busy waiting
await asyncio.sleep(0.001) # Add a small delay to avoid busy waiting

except WebSocketDisconnect:
print("Attempting to reconnect to client")
await asyncio.sleep(backoff)
Expand Down

0 comments on commit b0ef00d

Please sign in to comment.