Skip to content

Commit

Permalink
Improve websocket handling for performance reasons
Browse files Browse the repository at this point in the history
Hopefully this change will eliminate/reduce our H27 errors because we're closing old websocket connections
  • Loading branch information
Thrillberg committed Dec 21, 2024
1 parent 5e42cd3 commit 91c21fb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/javascript/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class APIClient {
this.ws = this.initws();
this.handlers = {};
this.messageQueue = [];
this.maxQueueSize = 100;
}

initws() {
Expand All @@ -21,7 +22,7 @@ class APIClient {
if (this.handlers[envelope.kind]) {
this.handlers[envelope.kind](envelope.data);
} else {
throw new Error(`unhandled kind: ${envelope.kind}`);
console.warn(`Unhandled kind: ${envelope.kind}`, envelope);

Check warning on line 25 in app/javascript/src/router/index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected console statement
}
},
});
Expand All @@ -30,7 +31,7 @@ class APIClient {
if (this.handlers[envelope.kind]) {
this.handlers[envelope.kind](envelope.data);
} else {
throw new Error(`unhandled kind: ${envelope.kind}`);
console.warn(`Unhandled kind: ${envelope.kind}`, envelope);

Check warning on line 34 in app/javascript/src/router/index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected console statement
}
},
});
Expand All @@ -40,12 +41,18 @@ class APIClient {
onclose(event) {
console.log(`WebSocket closed with code: ${event.code}, reason: ${event.reason}`);

Check warning on line 42 in app/javascript/src/router/index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected console statement
if (event.code !== 1000) {
this.ws?.connection?.webSocket?.close();

setTimeout(() => {
this.ws = this.initws();
}, 5000);
}
}

beforeunload() {
this.ws?.connection?.webSocket?.close();
}

send(data, channel) {
if (this.ws.connection.webSocket?.readyState === WebSocket.OPEN) {
const sendableData = {
Expand All @@ -55,6 +62,10 @@ class APIClient {
};
this.ws.send(sendableData);
} else {
if (this.messageQueue.length >= this.maxQueueSize) {
console.warn('Message queue is full, dropping message');

Check warning on line 66 in app/javascript/src/router/index.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected console statement
return;
}
this.messageQueue.push(data);
}
}
Expand Down

0 comments on commit 91c21fb

Please sign in to comment.